The original, happily working version of my project looked like this:
我项目的原始,愉快工作版本看起来像这样:
1) User fills out form (new action) and hits submit (create action)
1)用户填写表格(新动作)并点击提交(创建动作)
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
2)用户被重定向到他们的编辑页面(编辑操作使用由模型创建的edit_id,而不是Rails自动生成ID),这显示用户已经提交的信息
3) User can choose to change info (update action) and re-submit
3)用户可以选择更改信息(更新操作)并重新提交
In this version, even if the user changes nothing in the edit page and submits, the page will still flash a success alert.
在此版本中,即使用户在编辑页面中没有任何更改并提交,页面仍将闪烁成功警报。
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.
从数据库的角度来看,我并不在意,因为表单预先填充了用户的信息,update_attributes方法只是用相同的信息覆盖旧信息。
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
And adding one additional component to the "if" like this:
并在“if”中添加一个额外的组件,如下所示:
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?
但这不起作用。现在发生的是,在编辑页面上,如果我没有更改任何信息并点击提交,没有任何事情发生(这很好),但如果我改变信息并点击提交,仍然没有任何事情发生(这是不好的)。我究竟做错了什么?
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...
注意:我最初认为这是一个操作错误的顺序,所以我把它作为嵌套if,首先是if @ request.update_attributes,第二个if @ request.changed,但是这个也不起作用......
1 个解决方案
#1
20
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
:
update_attributes方法包含“save”调用作为其方法的一部分,如果保存成功则返回true。我认为你正在使用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
#1
20
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
:
update_attributes方法包含“save”调用作为其方法的一部分,如果保存成功则返回true。我认为你正在使用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