You have a nil object when you didn't expect it!

Hi,

I am creating a blog to learn ruby on rails.

I have 2 different views/models/controllers, 1 called post and 1 called
comments. How do i link a post from the views/post directory to the
comments that belong to the post in the views/comment directory.

This is the code in the post/show.rhtml

<%= render :partial => “post”, :object => @post %>

<%= link_to ‘Back’, :action => ‘list’ %>

Comments

<%= render :partial => “comment/comment”, :collection => @comments, :id
=> @post.id %>

This the code in the comment_comment.rhtml

<%= link_to comment.body, :action => 'show', :id => comment.post_id %>

I keep getting the “You have a nil object when you didn’t expect it!”
error message. I have tried everything i can think off.

Can anyone help?

If you linked the models together using has_many and belongs_to, you
could just do this:

<% for comment in @post.comments -%>
#your comment display code here
<% end -%>

The post model has this at the top:
has_many :comments

And the comment model has this:
belongs_to :post

I’m not sure about the separate controllers and views. But you may
have over separated things here. It’s hard for me to tell from your
snippets though. And I’m fairly new to rails also.

Hope that helps.

-Ron

On 4/1/06, John B. [email protected] wrote:

<%= render :partial => “post”, :object => @post %>

<%= link_to comment.body, :action => 'show', :id => _______________________________________________ Rails mailing list [email protected] http://lists.rubyonrails.org/mailman/listinfo/rails


Ron M.
[email protected]
413.329.4277

Thanks for your reply.

I have linked the models using the has_many and belongs to. I may have
over seperated them a bit so i have created a new project and put the
comments in the same post folder. Using the code you suggested displays
a post and the associated comments below.

<%= render :partial => “post”, :object => @post %>

<%= link_to ‘Edit’, :action => ‘edit’, :id => @post %> |
<%= link_to ‘Back’, :action => ‘list’ %>

Comments

<% for comment in @post.comments -%>

<% post.body %>

<% end %>

What i want is to have a _comment.rhtml file with a template to display
a comment so this template can be reused, and instead off having the for
loop to display the comments i will use a render partial like below

<%= render :partial => “comment”, :collection => @comments%>

I am still getting the same error message “You have a nil object when
you didn’t expect it!”

The render partial is pointing to the _comment.rhtml file but it doesn’t
know which comments to display because i think i need to to tell it
which comments to display belonging to the post.

_comments.rhtml is below

<%= link_to comment.body, :action => 'show', :id => comment %>

<%= comment.created_at.to_s(:long) %>


Ho do i pass the post id from the render partial to the _comment.rhtml
so it knows which comments to display?

I did try the loop code in the _comment.rhtml file but then i got the
following error:

“undefined local variable or method `post’”. it doesn’t know which
post’s comments to show.

To display my posts i have a file called _posts.rhtml code below.

<%= link_to post.title, :action => 'show', :id => post %>

<%= post.created_at.to_s(:long) %>

<%= post.body %>

(<%= link_to 'Edit', :action => 'edit', :id => post %>) (<%= link_to 'Destroy', { :action => 'destroy', :id => post }, :confirm => 'Are you sure?', :post => true %>)

in my file list.rhtml to display the posts all i have is the code below
and this displays all the post in the collection without the loop using
the _post.rhtml as a template. I like this as the _post.rhtml template
is reusable and that is what i want for the _comment.rhtml.

<%= render :partial => “post”, :collection => @posts.reverse %>

The above works perfectly so i have followed the same principal to
display the comments that belong to a post. The only difference is how
to let the _comments.rhtml know which post’s comments to display.

Its driving me mad! I cant find anything on the internet? Maybe you
cant do it but i cant see why not.

Ok, I think I understand. What I don’t see in your _comments partial is
the
loop. You are passing the @comments in, which should be @post.comments
I
think. And then in the partial, you need to run the loop. You are
referencing “comment”, but rails probably doesn’t know what that is yet,
since you have to divide the @post.comments into individual comment
objects.

So you are getting a nil value from either @comments, comment, or both.
So…

<%= render :partial => “comment”, :collection => @post.comments%>

I’m not sure on a couple points. Using the :collection syntax, what
does
the variable look like inside the partial? And more importantly, why
can’
that error message tell me WHICH object is nil? I really hate that
error.

-Ron

Yes that was just a typo.
the code Is

<% for comment in @post.comments -%>

<% post.body %>

<% end %>

As stated above this works when put in the _post.rhtml file but if i try
to sepaerate it out to _comment.rhtml so it is resuable i got the nil
object error.

<% for comment in @post.comments -%>

<% comment.body %>

<% end %>

Thanks Ron for your help, i should have spotted that myself! Time for a
break i think!

<%= render :partial => “comment”, :collection => @post.comments %>

Thanks again!

<…>

Comments

<% for comment in @post.comments -%>

<% post.body %>

<% end %>

Shouldn’t it be comment.body not post.body?

Or am I missing something?

Regards,
Rimantas

http://rimantas.com/

To move the comments to a new directory just simply use

<%= render :partial => “comment/comment”, :collection => @post.comments
%>

No problem. Glad I could help.