我知道這個問題已經被問了很多,但通常提出的解決方案是將config.active_record.whitelist_attributes
設置爲false。我已經試過了,仍然得到這個 問題:Rails質量分配問題
Can't mass-assign protected attributes: ingredient_attributes
我有兩個型號:recipe.rb和ingredient.rb。他們有一對多的關係,每個配方可以有很多成分。
recipe.rb
class Recipe < ActiveRecord::Base
attr_accessible :description, :name, :yield, :recipe_id
has_many :ingredient, :dependent => :destroy
accepts_nested_attributes_for :ingredient
end
ingredient.rb
class Ingredient < ActiveRecord::Base
belongs_to :recipe
attr_accessible :ingredient, :listorder, :recipe_id
end
出於好奇,你爲什麼宣佈':recipe_id''attr_accessible'在'Recipe'模式? – zeantsoi
我想我已經讀過這是一個解決大規模分配問題的方法,但它沒有起作用,所以我可以把它拿出來。 –
您收到的錯誤消息表示一對一關係,而不是一對多關係。但是,您的模型看起來正確。也許有些東西在處理你的表單時很腥......你可以發佈你的控制器和視圖嗎? – zeantsoi