Layout problem


#1

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!


#2

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


#3

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?


#4

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

<% =categorylist.name %>

jt


#5

Anyone?


#6

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?


#7

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!


#8

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.


#9

try this:

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

–r

2006/4/4, John B. removed_email_address@domain.invalid: