ga-wdi-exercises / project2

[project]
1 stars 30 forks source link

Controller issue #833

Closed Nykke closed 7 years ago

Nykke commented 7 years ago

I'm having a hard time getting my show, I'm getting this error


undefined method `each' for #<Category:0x007fd4d5470188>
Extracted source (around line #6):

  <% @category.each do |recipe| %>
    <div class="recipe">
      <h2><%= link_to recipe.title, recipe %></h2>
      <img src="<%= recipe.img_url %>">

this is what I have in my categories controller

def show
    @category = Category.find(params[:id])
    @recipe = Recipe.new
  end
avalant commented 7 years ago

Could you update your project 2 repo for me?

Nykke commented 7 years ago

I just updated and pushed it

avalant commented 7 years ago

I know we talked about this in person but, I'd recommend nesting your category and recipe routes.

routes.rb

  resources :categories do
    resources :recipes, only: [:index, :show]
  end

Then update your controller and your view accordingly. I think this will simplify the code you'll need for your views.

For your views, you'll pass in both your @receipe and @category.

For your controller itself, you should drop the Recipe.new and rather find the recipe based off of the relationship between the category id assigned to your recipes based off of the id of the category the user is clicking.

Nykke commented 7 years ago

got it fixed without nesting. I was calling the names wrong.

avalant commented 7 years ago

👍