我有食譜和成分在多對多的關係。紅寶石在鐵軌上 - 呈現多對多關係
我在演示文稿中定義了以下命令。
<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