rails控制器,未定义的方法错误 - 基于边缘指南

时间:2010-09-06 22:32:39

标签: ruby-on-rails

我正在建立一个相当简单的配方网站来学习RoR,我一直在关注入门指南,除了我已经交换了食谱和食材评论的帖子。 我一直在删除评论(成分)http://edgeguides.rubyonrails.org/getting_started.html#deleting-comments

现在我收到了错误

undefined method `recipe' for #

导致问题的部分行在这里

<%= link_to 'Delete Ingredient', [ingredient.recipe_id, ingredient],
                    :confirm => 'Are you sure',
                    :method => :delete %>

控制器方法(我认为没有任何影响,但我不完全确定)是

   
def destroy
        @recipe = Recipe.find(params[:recipe_id])
        @ingredient = @recipe.ingredient.find(params[:id])
        @ingredient.destroy
        redirect_to post_path(@recipe)
end

我在link_to中使用'recipe_id',因为当我输出调试时,它没有'recipe'属性,但有一个recipe_id属性。

调试的输出是


--- !ruby/object:Ingredient 
attributes: 
  id: 3
  ingredient: testing
  amount: 10
  measure: "10"
  description: "10"
  recipe_id: 2
  created_at: 2010-09-06 22:16:17.599217
  updated_at: 2010-09-06 22:16:17.599217
attributes_cache: {}

changed_attributes: {}

destroyed: false
marked_for_destruction: false
new_record: false
previously_changed: {}

readonly: false

我假设[ingredient.recipe_id,成分]只是变量的哈希值? 那是对的吗?我是从错误的角度来看这个吗?

1 个答案:

答案 0 :(得分:1)

在黑暗中拍摄,你的协会是否合适?有点像:

class Ingredient < ActiveRecord::Base
  belongs_to  :recipe
  ...

那会给你

@ingredient.recipe

希望有所帮助。