我在 食谱 和 成分 之间存在多对多关系。我正在尝试构建一个允许我向食谱添加成分的表单。
(这个问题的变体已经被反复询问,我花了几个小时来解决这个问题,但从根本上对 accepts_nested_attributes_for
的作用感到困惑。)
在你被下面的所有代码吓到之前,我希望你会看到它真的是一个基本问题。以下是不可怕的细节......
错误
当我显示一个表单来创建一个配方时,我收到错误“未初始化的常量 Recipe::IngredientsRecipe”,指向我的表单中的一行部分
18: <%= f.fields_for :ingredients do |i| %>
如果我改变这一行,使“成分”单数
<%= f.fields_for :ingredient do |i| %>
然后表单显示,但是当我保存时,我收到了一个批量分配错误
Can't mass-assign protected attributes: ingredient
。模型(在 3 个文件中,相应命名)
class Recipe < ActiveRecord::Base
attr_accessible :name, :ingredient_id
has_many :ingredients, :through => :ingredients_recipes
has_many :ingredients_recipes
accepts_nested_attributes_for :ingredients
accepts_nested_attributes_for :ingredients_recipes
end
class Ingredient < ActiveRecord::Base
attr_accessible :name, :recipe_id
has_many :ingredients_recipes
has_many :recipes, :through => :ingredients_recipes
accepts_nested_attributes_for :recipes
accepts_nested_attributes_for :ingredients_recipes
end
class IngredientsRecipes < ActiveRecord::Base
belongs_to :ingredient
belongs_to :recipe
attr_accessible :ingredient_id, :recipe_id
accepts_nested_attributes_for :recipes
accepts_nested_attributes_for :ingredients
end
Controller
作为
rails generate scaffold
生成的 RESTful 资源并且,因为“recipe”的复数形式不规则,
inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'recipe', 'recipes'
end
查看 (
recipes/_form.html.erb
)<%= form_for(@recipe) do |f| %>
<div class="field">
<%= f.label :name, "Recipe" %><br />
<%= f.text_field :name %>
</div>
<%= f.fields_for :ingredients do |i| %>
<div class="field">
<%= i.label :name, "Ingredient" %><br />
<%= i.collection_select :ingredient_id, Ingredient.all, :id, :name %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
环境
尝试了一些东西
如果我更改 View
f.fields_for :ingredient
然后表单加载(它正确地找到了 Recipe::IngredientRecipe
,但是当我保存时,我会收到如上所述的批量分配错误。这是日志Started POST "/recipes" for 127.0.0.1 at 2012-11-20 16:50:37 -0500
Processing by RecipesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"/fMS6ua0atk7qcXwGy7NHQtuOnJqDzoW5P3uN9oHWT4=", "recipe"=>{"name"=>"Stewed Tomatoes", "ingredient"=>{"ingredient_id"=>"1"}}, "commit"=>"Create Recipe"}
Completed 500 Internal Server Error in 2ms
ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: ingredient):
app/controllers/recipes_controller.rb:43:in `new'
app/controllers/recipes_controller.rb:43:in `create'
Controller 中的故障线路很简单
@recipe = Recipe.new(params[:recipe])
所以传递的参数,包括嵌套的属性,在某种程度上是不正确的。但是我已经尝试了很多变体,可以解决一个问题。我不明白什么?
最佳答案
多亏了所有人的线索,我发现了我的方法有什么问题。这是我解决它的方法。
我最初尝试使用简单的 HABTM 多对多关系,其中连接表按照标准 Rails 约定命名: ingredients_recipes
。然后我意识到在某种程度上,accepts_nested_attributes_for
是为一对多关系设计的。所以我转换为使用 has_many_through
,创建了一个模型 IngredientsRecipes
。
这个名字是核心问题,因为在使用 build
创建表单元素时,Rails 需要能够从复数转换为单数。这导致它寻找不存在的类 Recipe::IngredientsRecipe
。当我更改表单以便使用 fields_for :ingredient
时显示的表单,但仍然无法保存并出现批量分配错误。当我将 :ingredients_attributes
添加到 attr_accessible
时,它甚至失败了。当我将 @recipe.ingredients.build
添加到 RecipesController#new
时,它仍然失败。
将模型改为单数形式是解决问题的最终关键。 IngredientsRecipe
会起作用,但我选择了 RecipeIngredients
,因为它更有意义。
所以总结一下:
accepts_nested_attributes_for
与 has_and_belongs_to_many
一起使用;需要带有 has_many
选项的 through
。 (感谢@kien_thanh)accepts_nested_attributes_for
创建一个必须以 attr_accessible
的形式添加到 <plural-foreign-model>_attributes
的访问器,例如在 Recipe
我添加了 attr_accessible :name, :ingredients_attributes
(感谢@beerlington)new
方法中显示表单之前,必须在创建新实例后在外部模型上调用 build
,如 3.times { @recipe.ingredients.build }
。这导致 HTML 具有类似 recipe[ingredients_attributes][0][name]
的名称(感谢@bravenewweb)关于ruby-on-rails - Rails 3,多对多形式使用accepts_nested_attributes_for,我该如何正确设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13483057/