我有这样的模型
field :first_name, :type => String
field :last_name, :type => String
field :admin, :type => Boolean, :default => false
field :write_article, :type => Boolean, :default => false
现在,当我尝试使用
更新我的模型时@user = User.find(params[:id])
if @user.update_attributes(params[:user])
flash[:notice] = "Successfully updated User."
redirect_to users_path
else
render :action => 'edit'
end
write_article字段未更新。它总是错误的。我试图调试代码,我在params [:user]中得到了write_article = 1。但是,更新时仍将值设置为false。所以,我必须添加以下
@user.write_article = params[:user]['write_article']
在使用update_attributes调用之前显式设置该值。有什么建议吗?它应该没有上述行
答案 0 :(得分:1)
您的代码表明您没有使用ActiveRecord。如果您使用的是ActiveRecord以外的ORM,您可能需要提及您使用的ORM(例如,DataMapper,Mongoid)。
如果您的字段由于某种原因未更新,则可能是由于批量分配。
尝试添加attr_accessible :admin
。请参阅the details on the mass assignment issue at the Rails Guide。