0
votes

I'm building a fairly simple recipe site to learn RoR, and I've been following the getting started guide, except that I've exchanged posts for recipes and comments for ingredients. I got all the way to deleting a comment (ingredient) http://edgeguides.rubyonrails.org/getting_started.html#deleting-comments

now i'm getting an error

undefined method `recipe' for #

The line which in the partial which is causing the problem is here

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

The controller method (which I don't think has any effect, but I'm not completely sure) is

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

I use 'recipe_id' in the link_to because when I output the debug, it doesn't have a 'recipe' attribute, but has a recipe_id attribute.

The output of the debug is


--- !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

I'm assuming the [ingredient.recipe_id, ingredient] is simply a hash of the variables?? Is that correct? Am I coming at this from the wrong angle?

1
It sounds like what your trying to accomplish is a 'nested form'. An ingredient form, inside of a recipe form. I'd google that up. If you're still stuck, post your Model for the ingredients and recipes. How are they associated?Trip

1 Answers

1
votes

Shot in the dark, are your associations proper ? Kinda like:

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

That will give you

@ingredient.recipe

Hope that helps.