Forum: Ruby on Rails Layout problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
John B. (Guest)
on 2006-04-04 00:18
Hi,

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

<td width=150 valign="top">This is the left menu</td>
<td width=600 valign="top"><%= @content_for_layout %></td>
<td width=150 valign ="top"><%= 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.

<b>right template</b>
<% 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!
Dan P. (Guest)
on 2006-04-04 01:29
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
>
> <td width=150 valign="top">This is the left menu</td>
> <td width=600 valign="top"><%= @content_for_layout %></td>
> <td width=150 valign ="top"><%= 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.
>
> <b>right template</b>
> <% 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)

<b>right template</b>
<%= categorylist.name %>   #the local var is going to be named after the
partial

Dan
John B. (Guest)
on 2006-04-04 10:45
Thanks for yuor reply dan.
I have put the following code in _categorylist.rhtml
<b>right template</b>
<%= @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: <b>right template</b>
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?
John T. (Guest)
on 2006-04-04 11:30
(Received via mailing list)
Because, as Dan mentioned, inside your partial, _categorylist.rhtml,
the local variable is named after the partial. Use this instead:

<% =categorylist.name %>

jt
John B. (Guest)
on 2006-04-04 12:35
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: <b>right template</b>
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
<b>right template</b>
<%= 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?
John B. (Guest)
on 2006-04-04 17:31
Anyone?
Robert W. (Guest)
on 2006-04-04 18:00
(Received via mailing list)
try this:

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

--r

2006/4/4, John B. <removed_email_address@domain.invalid>:
John B. (Guest)
on 2006-04-04 18:07
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!
Tom A. (Guest)
on 2006-04-04 18:15
(Received via mailing list)
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.
This topic is locked and can not be replied to.