仅当属性已更改时才更新用户的属性 [英] Update attributes for User only if attributes have changed

查看:41
本文介绍了仅当属性已更改时才更新用户的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目的原始、愉快的工作版本如下所示:

The original, happily working version of my project looked like this:

1) 用户填写表单(新操作)并点击提交(创建操作)

1) User fills out form (new action) and hits submit (create action)

2) 用户被重定向到他们的编辑页面(编辑操作使用由模型创建的 edit_id,而不是 Rails 自动生成 id),显示用户已经提交的信息

2) User is redirected to their edit page (edit action uses an edit_id created by model, not Rails auto-gen id), which shows the info user had already submitted

3) 用户可以选择更改信息(更新操作)并重新提交

3) User can choose to change info (update action) and re-submit

在此版本中,即使用户在编辑页面中没有任何更改并提交,页面仍然会闪烁成功警报.

In this version, even if the user changes nothing in the edit page and submits, the page will still flash a success alert.

从数据库的角度来看,我并不在乎,因为表单预先填充了用户的信息,update_attributes 方法只是用相同的信息覆盖旧信息.

From a database perspective, I don’t really care, because since the form is prefilled with the user’s info, the update_attributes method is just overriding old info with the same info.

不过,从用户的角度来看,这很烦人,所以我想确保只有在用户实际更改某些内容时才更新信息并且成功警报才会闪烁.

From a user perspective though, it's annoying, so I want to ensure that the info is only updated and the success alert flashed only if the user actually changes something.

我认为这真的很容易,从这里更改旧代码:

I thought this would be really easy, changing the old code from this:

def update
  @request = Request.find_by_edit_id(params[:edit_id])
  if @request.update_attributes(request_params) 
    flash[:success] = true
    redirect_to edit_request_path(@request.edit_id)
  else
    render 'edit'
  end
end

并向if"添加一个额外的组件,如下所示:

And adding one additional component to the "if" like this:

def update
  @request = Request.find_by_edit_id(params[:edit_id])
  if @request.update_attributes(request_params) && @request.changed?
    flash[:success] = true
    redirect_to edit_request_path(@request.edit_id)
  else
    render 'edit'
  end
end

但这行不通.现在发生的情况是,在编辑页面上,如果我不更改任何信息并点击提交,则没有任何反应(这很好),但是如果我更改信息并点击提交,仍然没有任何反应(这很糟糕).我做错了什么?

But this doesn’t work. Now what happens is that, on the edit page, if I don’t change any info and hit submit, nothing happens (which is great), but if I DO change info and hit submit, still nothing happens (which is bad). What am I doing wrong?

注意:我最初认为这是操作顺序错误,所以我将其设为嵌套 if,首先是 if @request.update_attributes,其次是 if @request.changed,但这也不起作用......

Note: I initially thought it was an order of operations error, so I made it a nested if, with first if @request.update_attributes, and second if @request.changed, but this didn't work either...

推荐答案

update_attributes 方法包含save"调用作为其方法的一部分,如果保存成功则返回 true.我认为您正在使用 assign_attributes 寻找类似的东西:

The update_attributes method includes the 'save' call as part of its method and is returning true if the save is successful. I think you're looking for something like this using assign_attributes:

def update
  @request = Request.find_by_edit_id(params[:edit_id])
  @request.assign_attributes(request_params)

  if @request.changed?
    if @request.save
      flash[:success] = true
      redirect_to edit_request_path(@request.edit_id)
    else
      render 'edit'
    end
  else
    # Action if no change has been made
  end
end

这篇关于仅当属性已更改时才更新用户的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆