2011-03-29 70 views
9

我得到這個錯誤「警告:不能大規模指派保護屬性: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 

什麼想法?例如,

回答

10

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

19
比使用 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

相關問題