two controllers in one layout, rails 3

Posted by Grizlord on Stack Overflow See other posts from Stack Overflow or by Grizlord
Published on 2011-03-09T19:37:58Z Indexed on 2011/03/10 0:10 UTC
Read the original article Hit count: 272

Filed under:
|
|

Okay, I have two models, a recipe model and a category model. In my layout(application.html.erb) I have a main container div that "yields" the recipes index action. I'm trying to list all the category names as links in a side bar(also a div) by iterating over them in an unordered list. When you click one of the links it will go to the category show page which will then list all the recipes in that category.

Here is how I'm trying to list the links in -

<div class="container" id="categories">
<% for category in @categories %>
  <ul>
    <li><%= link_to category.name, category %></li>
  </ul>
<% end %>
</div>

The problem is I get a NoMethodError -

You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.each

It is not retrieving the records from the model. Any suggestions on how to get this done would be greatly appreciated. I tried to render a partial as some of the other similar posts have said but still get the same error.

This is the exact error -

NoMethodError in Recipes#index

Showing /Users/grizlord/Rails/recipe2/app/views/layouts/application.html.erb where line #39 raised:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

Extracted source (around line #39):

36:     </div>
37:     <div class="container" id="categories">
38:       Browse by Category
39:       <% for category in @categories %>
40:         <ul>
41:           <li><%= link_to category.name, category %></li>
42:         </ul>

© Stack Overflow or respective owner

Related posts about ruby-on-rails-3

Related posts about layout