【问题标题】:rails - how do I display many to many data model created with many to many association model in the ui?rails - 如何在 ui 中显示使用多对多关联模型创建的多对多数据模型?
【发布时间】:2012-06-09 21:47:30
【问题描述】:

我有一个食谱、成分、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

在我的用户界面中,这不再起作用了

<% @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 >

【问题讨论】:

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


    【解决方案1】:

    变化:

    has_many :Ingredients, :through => :RecipeIngredient
    

    has_many :ingredients, :through => :ingredient_recipes
    

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

    对于:食谱:

    has_many :recipes, :through => :ingredient_recipes 
    

    【讨论】:

    • 谢谢。现在我得到 - 找不到关联:RecipeIngredient in model Recipe on row (调试时我也看到@associationHAsh=Empty Hash)
    • 做显而易见的事情并将其更改为成分配方我收到无法评估的消息
    • 您应该使用 IngredientRecipie,因为这是 Rails 约定。确保遵守约定。试试看,看看是否有错误。如果你这样做,那将是另一回事。
    • 请发布完整的错误消息文本,“无法评估”没有足够的信息供我们诊断,以及发生这种情况的控制器线路。
    • 感谢您抽出宝贵时间 - 预期 D:/path.../ingredient_recipe.rb 来定义成分配方(我的关联类已定义类成分_配方
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    相关资源
    最近更新 更多