rails - 如何在ui中显示由多对多关联模型创建的多对多数据模型?

时间:2012-06-09 13:07:27

标签: ruby-on-rails ruby-on-rails-3

我有配方,成分,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 >

1 个答案:

答案 0 :(得分:1)

变化:

has_many :Ingredients, :through => :RecipeIngredient

has_many :ingredients, :through => :ingredient_recipes

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

对于:食谱:

has_many :recipes, :through => :ingredient_recipes