我有一个简单的模型:
class Reply < ActiveRecord::Base
attr_accessible :body
belongs_to :post
end
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我有一个简单的更新方法:
def update
@reply = Reply.find(params[:id])
if @reply.update_attributes!(params[:reply])
render :js => "alert('I am trying to update!')"
else
render :js => "alert('<%= @reply.errors %>')"
end
end
Run Code Online (Sandbox Code Playgroud)
这不会引发错误,但它实际上也不会更新回复.相反,我得到了"我正在尝试更新!" 消息,就像一切都有效.但是当我重新加载页面并查看回复时,它具有相同的文本.它实际上没有更新.如果我用以下内容替换update_attributes:
@reply.update_column(:body, params[:reply][:body])
Run Code Online (Sandbox Code Playgroud)
它工作正常.如果我使用:
@reply.update_attribute(:body, params[:reply][:body])
Run Code Online (Sandbox Code Playgroud)
它再一次无效.知道发生了什么事吗?
在我的日志中,我有这个:
Started PUT "/posts/2/replies/20" for 127.0.0.1 at 2013-01-19 10:39:57 -0600
Processing by RepliesController#update as JS
Parameters: {"utf8"=>"?", "authenticity_token"=>"Xot7E+ldXiBm0hVvw5XUP/U5guJU2g8e4QaLbDVGzDE=", "reply"=>{"body"=>"Updated text."}, "commit"=>"Submit Revision", "post_id"=>"2", "id"=>"20"
[1m[35mUser Load (1.0ms)[0m SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1 …
Run Code Online (Sandbox Code Playgroud)