我有食谱和成分在多对多的关系。红宝石在铁轨上 - 呈现多对多关系
我在演示文稿中定义了以下命令。
<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
我相信@ingredient是零,因为你的控制器的行为正在发生。你介意用那个编辑你的文章吗? – DaMainBoss
谢谢。但它确实显示了我最后一次尝试的成分 - @ recipe.ingredients.each。这是否意味着我的成分在那里?我有控制器配方和ingredienet和他们的模型和IngredientRecipe了。我应该在编辑中添加什么方法? – Jeb