我有3个嵌套模型:
class Recipe < ActiveRecord::Base
has_many :recipe_ingredients
has_many :ingredients, :through => :recipe_ingredients
accepts_nested_attributes_for :ingredients
accepts_nested_attributes_for :recipe_ingredients, :allow_destroy => true
attr_accessible :description, :name, :preparation, :recipe_ingredients_attributes, :ingredients_attributes
validates_presence_of :name, :description, :preparation
end
class RecipeIngredient < ActiveRecord::Base
belongs_to :recipe, :foreign_key => "recipe_id"
belongs_to :ingredient, :foreign_key => "ingredient_id"
accepts_nested_attributes_for :ingredient
delegate :name, :to => :ingredient, :allow_nil => true
attr_accessible :ingredient_id, :quantity, :recipe_id, :ingredient_attributes
end
class Ingredient < ActiveRecord::Base
has_many :recipe_ingredients
has_many :recipes, :through => :recipe_ingredients
attr_accessible :name
end
这是我的食谱控制器:
#<snip>
def new
@recipe = Recipe.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @recipe }
end
end
def create
@recipe = Recipe.new(params[:recipe])
respond_to do |format|
if @recipe.save
format.html { redirect_to @recipe, notice: 'Het recept werd aangemaakt.' }
format.json { render json: @recipe, status: :created, location: @recipe }
else
format.html { render action: "new" }
format.json { render json: @recipe.errors, status: :unprocessable_entity }
end
end
end
#</snip>
这就是我的表单(使用simple_forms和nested_forms gems):
= simple_nested_form_for @recipe, :html => { :class => 'form-horizontal' } do |f|
= f.input :name, :label => "Naam"
= f.input :description, :input_html => { :class => "span6" }, :label => "Beschrijving"
= f.input :preparation, :input_html => { :class => "span6" }, :label => "Bereiding"
= f.simple_fields_for :recipe_ingredients do |ri|
= ri.input :quantity, :label => "#"
= ri.input :name, :label => "Ingredient"
= ri.link_to_remove "ingredient verwijderen"
%p
= f.link_to_add "Voeg ingredient toe", :recipe_ingredients
.form-actions
= f.submit "Update recept", :class => 'btn btn-primary'
= link_to t('.cancel', :default => "Annuleer"), recipes_path, :class => 'btn'
每当我保存食谱时,它都会给我: RecipesController #create中的ActiveModel :: MassAssignmentSecurity :: Error 无法批量分配受保护的属性:名称
这是我的堆栈跟踪:
Started POST "/recipes" for 127.0.0.1 at 2012-12-15 19:14:38 +0100
Processing by RecipesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"72F9E5Cim3SFlFabViy8p4eF4el+RGtdlPmkuaoBU90=", "recipe"=>{"name"=>"tets", "description"=>"egwrb", "preparation"=>"gwrv", "recipe_ingredients_attributes"=>{"1355595249889"=>{"quantity"=>"21", "name"=>"grsd", "_destroy"=>"false"}}}, "commit"=>"Update recept"}
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Completed 500 Internal Server Error in 2ms
ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: name):
app/controllers/recipes_controller.rb:54:in `new'
app/controllers/recipes_controller.rb:54:in `create'
我尝试过单数/复数但没有运气。我不知道我做错了什么。 :ingredient_attributes应该是可访问的,但似乎不是?
亲切的问候, 史蒂芬
答案 0 :(得分:0)
实际上,我认为你的观点中只有一个小错字:
= f.simple_fields_for :ingredient do |i|
应该是
= ri.simple_fields_for :ingredient do |i|
我注意到,当它应该嵌套在recipe_ingredients_attributes
下时,params hash在顶层有成分。 (您可能还需要在accepts_nested_attributes_for
)上设置RecipeIngredient
答案 1 :(得分:0)
您必须创建一组额外的嵌套字段。在ri
区块内,您需要添加
- ri.simple_fields_for :ingredient do |i|
= i.input :name
通过这样做,您可以正确设置ingredient
的名称,而不是recipe_ingredient
。