我有一个多对多关系的配方和成分。
我在演示文稿中定义了以下命令。
<div>
<%= render :partial => 'ingredients/form',
:locals => {:form => recipe_form} %>
</div>
部分以
<%= form_for(@ingredient) do |ingredient_form| %>
但收到@ingredient nill。然后我尝试了
<%= recipe_form.fields_for :ingredients do |builder| %>
<%= render 'ingredient_fields', f: builder %>
<% end %>
我的渲染在哪里
<p class="fields">
<%= f.text_field :name %>
<%= f.hidden_field :_destroy %>
</p>
但没有打印任何内容。然后我尝试了
<% @recipe.ingredients.each do |ingredient| %>
<%= ingredient.name %>
<% end %>
只有这样所有的成分都被打印出来了。我在以前的尝试中做错了什么?谢谢你。
我的配料配方关系定义如下
class Ingredient < ActiveRecord::Base
has_many :ingredient_recipes
has_many :recipes, :through => :ingredient_recipes
...
class Recipe < ActiveRecord::Base
has_many :ingredient_recipes
has_many :ingredients, :through => :ingredient_recipes
...
accepts_nested_attributes_for :ingredient_recipes ,:reject_if => lambda { |a| a[:content].blank?}
class IngredientRecipe < ActiveRecord::Base
attr_accessible :created_at, :ingredient_id, :order, :recipe_id
belongs_to :recipe
belongs_to :ingredient
end