rails - 如何显示 UI 中用多对多关联模型创建的多对多数据模型



我有食谱、成分Ingredient_Recipe模型

食谱有

has_many :ingredient_recipes
has_many :Ingredients, :through => :RecipeIngredient

成分有

has_many :ingredient_recipes
has_many :Recipes, :through => :RecipeIngredient 

Ingredient_Recipe有

belongs_to :recipes
belongs_to :ingredients

在我的 UI 中,这不再有效

<% @recipe.ingredients.each do |ingredient| %>

编辑

  ActionView::Template::Error (uninitialized constant Recipe::Ingredients):  
97:                       </td>
98:                     <tr>
99:                       <td >
100:                         <% @recipe.ingredients.each do |ingredient| %>
101:                             ingredient.name
102:                         <% end %>
103:                       </td >

更改:

has_many :Ingredients, :through => :RecipeIngredient

has_many :ingredients, :through => :ingredient_recipes

不要大写 :ingredients,并且 :through 需要引用您正在经历的关联而不是模型。

对于 :食谱:

has_many :recipes, :through => :ingredient_recipes 

最新更新