如何防止在Rails中更新单个属性? [英] How to prevent updating a single attribute in Rails?
问题描述
提交表单后,如何防止在Rails中更新单个属性?其他所有属性都应更新.
When a form is submitted, how to prevent a single attribute from being updated in Rails? All other attributes should be updated.
是before_save
,attr_reader
还是其他方式?
如果使用before_save
,如何访问属性哈希?
If using before_save
, how to access to the attributes hash?
Rails 3.0.7
Rails 3.0.7
推荐答案
签出 现在,作为对update_attributes
的调用的一部分,您将指定:update
角色,例如
Now as part of a call to update_attributes
, you'd specify the :update
role, for example
klass = YourModel.find(some_id)
klass.update_attributes(params[:your_model], as: :update)
如果在传递给update_attributes
的params
中设置了:the_one_column
,则会引发错误.
If :the_one_column
is set in params
passed to update_attributes
, it will throw an error.
正如@Beerlington在对您的问题的评论中提到的那样,您还应该查看
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 with attr_protected
.
这篇关于如何防止在Rails中更新单个属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!