【发布时间】:2018-12-24 14:44:13
【问题描述】:
我有三个模型,成分,recipe_ingredient 和 recipy
class Ingredient < ApplicationRecord
has_many :recipe_ingredients
end
class RecipeIngredient < ApplicationRecord
belongs_to :recipy, :dependent => :destroy
belongs_to :ingredient
end
class Recipy < ApplicationRecord
has_many :recipy_steps
has_many :recipe_ingredients, :dependent => :delete_all
end
我正在尝试从食谱显示页面访问成分表中的 ing_name 属性。
<% @recipe_ingredients.each do |ing| %>
<p> <%= ing.amount %> <%= ing.unit %>
<%= ing.ingredient.ing_name %>
</p>
从recipies 控制器中显示:
def show
@recipe_ingredients = @recipy.recipe_ingredients
end
但我不断收到以下错误消息: nil:NilClass 的未定义方法“ing_name”
我的成分参数:
def ingredient_params
params.require(:ingredient).permit(:ing_name)
end
它似乎确实像这样工作:
<%= Ingredient.where(id: ing.ingredient_id).pluck(:ing_name) %>
但是,如果我理解正确,这不会使用表之间的连接吗?有什么帮助吗?谢谢。
【问题讨论】:
-
您遇到的错误是什么
-
。 nil:NilClass 的未定义方法 `ing_name' 但已解决