我有一个名为 Discussion 的简单模型,其中有一个名为已解决的 boolean 列。
在我的表单中,我有以下代码
<%= form_for(@discussion) do |d| %>
...
<%= d.check_box :resolved %>
<% end %>
在我的 Controller 中,我有以下内容:
def update
@discussion = Discussion.find(params[:id])
if @discussion.update_attributes(params[:discussion])
etc...
end
end
当我提交表单时,我可以看到参数正在发送到服务器......
Parameters: {"utf8"=>"✓", "authenticity_token"=>"AsGsRHwiVva/+kTrBs0IjLeZwj1ZmXBuKZr9Pg/N6Xk=", "discussion"=>{"shortdesc"=>"Talk about something.", "content"=>"Try to update check box.", "resolved"=>"1"}, "commit"=>"Update Discussion", "id"=>"1"}
但查询不包括任何关于更新该字段的内容。
AREL (14.9ms) UPDATE "discussions" SET "content" = 'Try to update check box.', "updated_at" = '2011-07-18 17:53:50.783176' WHERE "discussions"."id" = 1
知道我缺少什么吗?
最佳答案
发生这种情况的原因有 4 个:
resolved
已在数据库中设置为 true。 resolved=
模型中的方法,它不再设置属性。 attr_protected :resolved
. attr_accessible
但没有:resolved
在列表中。 关于ruby-on-rails - rails : update_attributes not updating all attributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6737133/