Forum: Ruby on Rails You have a nil object when you didn't expect it!

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-01 15:20
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' %>

<h2>Comments</h2>

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

This the code in the comment_comment.rhtml
<div>
   <h2> <%= link_to comment.body, :action => 'show', :id =>
comment.post_id %></h2>
 </div>

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?
Ron M. (Guest)
on 2006-04-01 17:11
(Received via mailing list)
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. <removed_email_address@domain.invalid> wrote:
> <%= render :partial => "post", :object => @post %>
>    <h2> <%= link_to comment.body, :action => 'show', :id =>
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Ron M.
removed_email_address@domain.invalid
413.329.4277
John B. (Guest)
on 2006-04-01 17:25
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' %>

<h2>Comments</h2>

<% for comment in @post.comments -%>
  <p><% post.body %><p>
<% 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

<div>
    <p><%= link_to comment.body, :action => 'show', :id => comment
%></p>
    <p><i><%= comment.created_at.to_s(:long) %></i></p>
    <hr />
</div>

Ho do i pass the post id from the render partial to the _comment.rhtml
so it knows which comments to display?
Ron M. (Guest)
on 2006-04-01 17:45
(Received via mailing list)
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
John B. (Guest)
on 2006-04-01 18:06
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.
 <div>
    <h2><%= link_to post.title, :action => 'show', :id => post %></h2>
    <p><i><%= post.created_at.to_s(:long) %></i></p>
    <p><%= post.body %> </p>
    (<%= link_to 'Edit', :action => 'edit', :id => post %>)
    (<%= link_to 'Destroy', { :action => 'destroy', :id => post },
:confirm => 'Are you sure?', :post => true %>)</p>
</div>

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.
Rimantas L. (Guest)
on 2006-04-01 18:15
(Received via mailing list)
<...>
> <h2>Comments</h2>
>
> <% for comment in @post.comments -%>
>   <p><% post.body %><p>
> <% end %>

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

Or am I missing something?

Regards,
Rimantas
--
http://rimantas.com/
John B. (Guest)
on 2006-04-01 18:22
Yes that was just a typo.
the code Is

<% for comment in @post.comments -%>
  <p><% post.body %><p>
<% 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.
John B. (Guest)
on 2006-04-01 18:23
<% for comment in @post.comments -%>
  <p><% comment.body %><p>
<% end %>
John B. (Guest)
on 2006-04-01 18:35
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!
John B. (Guest)
on 2006-04-01 18:42
To move the comments to a new directory just simply use


<%= render :partial => "comment/comment", :collection => @post.comments
%>
Ron M. (Guest)
on 2006-04-01 19:36
(Received via mailing list)
No problem.  Glad I could help.
This topic is locked and can not be replied to.