# Layout problem

**URL:** <https://www.ruby-forum.com/t/layout-problem/93480>\
**Category:** Rails\
**Created:** [April 3, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/layout-problem/93480 "2006-04-03T22:18:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 3, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/layout-problem/93480/1 "2006-04-03T22:18:53Z")

</div>

Hi,

I am creating a blog to learn ruby on rails. The blog.rhtml in the  
layout folder has a the following code

This is the left menu \<%= @content\_for\_layout %\> \<%= render :partial =\> "categorylist", :collection =\> @categories %\>

The content in the middle td displaying the posts is working fine. In  
the right td i want to display all the categories. I can get the  
template to render to the right column with just html but i cant display  
any of the categories when i add the ruby code to display them. The  
code for the \_categorylist.rhtml is below.

**right template**  
\<% for category in @categories %\>  
\<%= category.name %\>  
\<% end %\>

I keep getting the following error below:  
You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occured while evaluating nil.each

What is the problem? I have used similar code to get the posts working  
but cant seem to get this to work. Am i missing something in the blog  
controller. At the moment i have the following code in the  
blog\_controller:

def categorylist  
@categories = Category.find(:all)  
end

Can anyone help please!

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 3, 2006, 11:29pm UTC](https://www.ruby-forum.com/t/layout-problem/93480/2 "2006-04-03T23:29:56Z")

</div>

John B. wrote:

> Hi,
> 
> I am creating a blog to learn ruby on rails. The blog.rhtml in the  
> layout folder has a the following code
> 
> This is the left menu \<%= @content\_for\_layout %\> \<%= render :partial =\> "categorylist", :collection =\> @categories %\>
> 
> The content in the middle td displaying the posts is working fine. In  
> the right td i want to display all the categories. I can get the  
> template to render to the right column with just html but i cant display  
> any of the categories when i add the ruby code to display them. The  
> code for the \_categorylist.rhtml is below.
> 
> **right template**  
> \<% for category in @categories %\>  
> \<%= category.name %\>  
> \<% end %\>
> 
> I keep getting the following error below:  
> You have a nil object when you didn’t expect it!  
> You might have expected an instance of Array.  
> The error occured while evaluating nil.each
> 
> What is the problem? I have used similar code to get the posts working  
> but cant seem to get this to work. Am i missing something in the blog  
> controller. At the moment i have the following code in the  
> blog\_controller:
> 
> def categorylist  
> @categories = Category.find(:all)  
> end
> 
> Can anyone help please!
> 
> @categories is nil. in your code, each entry in @categories is being  
> passed to the partial as categorylist.
> 
> You’re looping through the @categories inside your partial. No need to  
> do that because you’re calling the partial with :collection . Each  
> category is being passed to the partial as a local, just use the partial  
> to display it. Maybe something like this: (may want to change the  
> partial name though)
> 
> **right template**  
> \<%= categorylist.name %\> #the local var is going to be named after the  
> partial
> 
> Dan

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 4, 2006, 8:45am UTC](https://www.ruby-forum.com/t/layout-problem/93480/3 "2006-04-04T08:45:50Z")

</div>

Thanks for yuor reply dan.  
I have put the following code in \_categorylist.rhtml  
**right template**  
\<%= @category.name %\>

I have tried what you suggested and i got the error below:

showing app/views/blog/\_categorylist.rhtml where line #2 raised:

undefined local variable or method `category’ for  
#\<#Class:0x38e1938:0x38e18f0\>

Extracted source (around line #2):

1: **right template**  
2: \<%= category.name %\>

I got the nil object error when i out the @ before category  
@category.name

I know this the way it is normally done as i have similar code to  
display the posts using partial. Is it something to do with the fact  
that i am trying to render from the layout?

Any ideas?

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 4, 2006, 9:30am UTC](https://www.ruby-forum.com/t/layout-problem/93480/4 "2006-04-04T09:30:17Z")

</div>

Because, as Dan mentioned, inside your partial, \_categorylist.rhtml,  
the local variable is named after the partial. Use this instead:

\<% =categorylist.name %\>

jt

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 4, 2006, 3:31pm UTC](https://www.ruby-forum.com/t/layout-problem/93480/5 "2006-04-04T15:31:39Z")

</div>

Anyone?

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 4, 2006, 10:35am UTC](https://www.ruby-forum.com/t/layout-problem/93480/6 "2006-04-04T10:35:00Z")

</div>

I have tried that and i get the following error:

You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.name

Extracted source (around line #2):

1: **right template**  
2: \<%= categorylist.name %\>

I have this exact code working for the posts. The only difference is  
the on the layout blog.rhtml i have

\<%= @content\_for\_layout %\>

in the index.rhtml page i have  
\<%= render :partial =\> “post”, :collection =\> @posts.reverse %\>

In the blog\_controller i have  
def index  
@posts = Post.find(:all)  
end

In the \_post.rhtml i have  
\<%= post.body %\>

The only difference in the categories i can see is in the blog.rhtml  
layout i have  
\<%= render :partial =\> “categorylist”, :collection =\> @categories %\>

In the \_category.rhtml i have  
**right template**  
\<%= categorylist.name %\>

In the blog\_controller i have  
def categorylist  
@categories = Category.find(:all)  
end

The main differences are index.rhtml doesn’t have an underscore before  
it, categorylist.rhtml does.

The blog controller uses the code below to get the collection of posts  
def index  
@posts = Post.find(:all)  
end

I think what the problem is the code below is not returning the  
categories collection, i have tried putting :action =\> ‘categorylist’  
but with no success but i stillcant figure out the problem!

def categorylist  
@categories = Category.find(:all)  
end  
Any other ideas?

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 4, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/layout-problem/93480/7 "2006-04-04T16:07:31Z")

</div>

Thanks robert that worked.

I had to change the categorylist def in the blog controller to get it to  
work

def categorylist  
@category = Category.find(params[:id])  
end

Theres no stopping me now!

Thanks again!

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 4, 2006, 4:15pm UTC](https://www.ruby-forum.com/t/layout-problem/93480/8 "2006-04-04T16:15:44Z")

</div>

What he said. Every time you refer to an instance variable in your  
template,  
be it in a partial, a layout, or an ordinary view, you need to make sure  
that instance variable exists and is populated. So even if you’re  
viewing  
@posts, you need to also add @categories if you want to list the  
categories  
somewhere on that page.

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 4, 2006, 4:00pm UTC](https://www.ruby-forum.com/t/layout-problem/93480/9 "2006-04-04T16:00:23Z")

</div>

try this:

def index  
@posts = Post.find(:all)  
@categories = Category.find(:all)  
end

–r

2006/4/4, John B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
