WARNING: Can't mass-assign protected attributes

前端 未结 2 1010
醉梦人生
醉梦人生 2021-02-06 00:42

I get this error \"WARNING: Can\'t mass-assign protected attributes: races_attributes\" , when following this http://railscasts.com/episodes/196-nested-model-form-part-1 on rail

相关标签:
2条回答
  • 2021-02-06 00:55

    attr_accessible specifies that you can not mass-assign attributes, using save method, for example. So, if you change an attribute that is not defined with attr_accessible, you will get a warning because it will not actually be saved in the database.

    0 讨论(0)
  • 2021-02-06 01:09

    Shorter than using attr_accessible, safer than using whitelist_attributes: attr_protected

    Just indicate the protected attributes, and Rails will infer that all others can be mass-assigned:

    class MyClass < ActiveRecord::Base
      attr_protected :id
    end
    

    (I always have way more attributes that I want mass-assigned than the ones I want protected.)

    0 讨论(0)
提交回复
热议问题