Problems with update_attributes for boolean field
I have my model like this
field :first_name, :type => String
field :last_name, :type => String
field :admin, :type => Boolean, :default => false
field :write_article, :type => Boolean, :default => false
Now, when I try to update my model with
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
flash[:notice] = "Successfully updated User."
redirect_to users_path开发者_Python百科
else
render :action => 'edit'
end
the write_article field is not updated. It is always false. I tried to debug the code and I do get write_article = 1 in the params[:user]. However when updating the value is still set to false. So, I had to add the following
@user.write_article = params[:user]['write_article']
explicitly set the value before using update_attributes call. Any suggestion? It should have worked without the above line
Your code indicates that you aren't using ActiveRecord. If you are using an ORM other than ActiveRecord, you might want to mention which one you use (e.g., DataMapper, Mongoid).
If your field is not updated for some reason, that might be due to mass assignment.
Try adding attr_accessible :admin
. See the details on the mass assignment issue at the Rails Guide.
精彩评论