警告:不能批量分配属性保护

问题描述:

我得到这个错误“警告:不能大规模指派保护属性:races_attributes” ,下面当这http://railscasts.com/episodes/196-nested-model-form-part-1上轨3警告:不能批量分配属性保护

凡种族是组件事件。这是我的模型/ race.rb:

class Race < ActiveRecord::Base 
belongs_to :event 

attr_accessible :name, :unit 
end 

这是我的模型/ event.rb:

class Event < ActiveRecord::Base 
has_many :races, :dependent => :destroy 

accepts_nested_attributes_for :races 

attr_accessible :name, :date, :description, :location_name, :address_one, :address_two, :city, :state, :zip, :active, :races_attributes 
end 

什么想法?例如,

attr_accessible指定不能整体分配属性,例如使用save方法。因此,如果您更改未使用attr_accessible定义的属性,则会收到警告,因为它实际上不会保存在数据库中。

+0

我已经将表单中的所有属性添加到每个attr_accessib le – Hosemeyer 2011-03-29 02:37:57

+0

哎呀,我忘了event_id之一。该死的! – Hosemeyer 2011-03-29 02:40:33

比使用 attr_accessible

更短,更安全比使用whitelist_attributesattr_protected

只是表示保护属性,Rails会推断出所有其他可批量分配:

class MyClass < ActiveRecord::Base 
    attr_protected :id 
end 

(我总是有更多的属性,我想批量分配比我想要保护的。)

+0

仅供参考,可以在application.rb中设置whitelist_attributes:'config.active_record.whitelist_attributes = true',以防您想要快速入侵并且无需修改单个模型。只需注释掉该行。 – qix 2013-10-15 16:37:11