如何防止在Rails中更新单个属性? [英] How to prevent updating a single attribute in Rails?

查看:110
本文介绍了如何防止在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_saveattr_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_attributesparams中设置了: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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆