简体   繁体   English

无法从关联的模型导轨访问属性

[英]Cannot access attributes from associated model rails

I have three models, ingredient, recipe_ingredient and 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

I am trying to access the ing_name attribute in the ingredients table from recipies show page. 我正在尝试从食谱显示页面访问成分表中的ing_name属性。

<% @recipe_ingredients.each do |ing| %>
<p> <%= ing.amount  %> <%= ing.unit %>
<%= ing.ingredient.ing_name %>
</p>

def Show from the recipies controller: def从收款控制器显示:

def show
 @recipe_ingredients = @recipy.recipe_ingredients
end

But I keep receiving the following error msg: undefined method `ing_name' for nil:NilClass 但是我一直收到以下错误消息:nil:NilClass的未定义方法“ ing_name”

My ingredient_params: 我的Ingredient_params:

def ingredient_params
params.require(:ingredient).permit(:ing_name)
end

It does seem to work like this: 它看起来确实像这样工作:

    <%= Ingredient.where(id: ing.ingredient_id).pluck(:ing_name) %>

But this does not use the connection between the tables if I understand correctly? 但是,如果我理解正确的话,这不会使用表之间的连接吗? Any help? 有什么帮助吗? Thanks. 谢谢。

You have ingredient nil thats why you got the error. 您有nil成分,这就是为什么您出错了。

Must be your controller has some before_action hook to load recipy 必须是您的控制器具有一些before_action钩子才能加载配方

class RecipesController < ApplicationController
  before_action :load_recipy, only: :show

  def show
    @recipe_ingredients = @recipy.recipe_ingredients
  end

 private
 def load_recipy
   @recipy = Recipy.find(params[:id])
  end
end

You can try this to avoid this nil error( undefined method 'ing_name' for nil:NilClass ) 您可以try避免这种nil错误( undefined method 'ing_name' for nil:NilClass

<% @recipe_ingredients.each do |ing| %>
<p> <%= ing.amount  %> <%= ing.unit %>
<%= ing.try(:ingredient).try(:ing_name) %>
</p>

From Rails 5 by default you got one required option to make ingredient always not nullable 默认情况下,从Rails 5获得一个required选项,以使成分始终不可为nullable

like below 像下面

belongs_to :ingredient, required: true

It will also prevent this error of 这也将防止此错误

class RecipeIngredient < ApplicationRecord
 belongs_to :recipy, :dependent => :destroy
 belongs_to :ingredient, required: true
end

the problem is because inside your show method @recipy is nil, here is usually code for show 问题是因为在您的show方法@recipy内部为零,这通常是show的代码

controller 控制者

def show
  @recipy = Recipy.find(params[:id]) # --> you missed this line
  @recipe_ingredients = @recipy.recipe_ingredients # @recipy will be null without line above
end

view 视图

<% @recipe_ingredients.each do |ing| %>
  <p> <%= ing.amount  %> <%= ing.unit %> <%= ing.ingredient.ing_name %> </p>
<% end %>

I would like also add some suggestion to your model relationship as follow since Ingredient and Recipy shows many to many relationship 我还想向您的模型关系添加一些建议,因为成分和配方显示了多对多关系

class Ingredient < ApplicationRecord
  # -> recipe_ingredients -> recipies
  has_many :recipe_ingredients, :dependent => :destroy
  has_many :recipies, through: :recipe_ingredients
end

class RecipeIngredient < ApplicationRecord
 belongs_to :recipy
 belongs_to :ingredient
end

class Recipy < ApplicationRecord
  has_many :recipy_steps
  # -> recipe_ingredients -> ingredients
  has_many :recipe_ingredients, :dependent => :destroy
  has_many :ingredients, through: :recipe_ingredients
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM