When a form is submitted, how to prevent a single attribute from being updated in Rails? All other attributes should be updated.
Is it before_save
, attr_reader
or some other way?
If using before_save
, how to access to the attributes hash?
Rails 3.0.7
Other option is simply doing this in your controller:
Check out
attr_protected
.Now as part of a call to
update_attributes
, you'd specify the:update
role, for exampleIf
:the_one_column
is set inparams
passed toupdate_attributes
, it will throw an error.As @Beerlington mentioned in his comment to your Question, you should also check out attr_accessible. It's generally better to spend the 30 minutes going through all models of your application white-listing attributes using
attr_accessible
than it is to blacklist specific attributes withattr_protected
.