John Butler
2006-Apr-01 11:20 UTC
[Rails] 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'' %> <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? -- Posted via http://www.ruby-forum.com/.
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 Butler <JohnnyButler7@gmail.com> wrote:> 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? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Ron Miles ronald.miles@gmail.com 413.329.4277
John Butler
2006-Apr-01 13:25 UTC
[Rails] Re: You have a nil object when you didn''t expect it!
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? -- Posted via http://www.ruby-forum.com/.
Ron Miles
2006-Apr-01 13:42 UTC
[Rails] Re: You have a nil object when you didn''t expect it!
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 On 4/1/06, John Butler <JohnnyButler7@gmail.com> wrote:> > 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? > > > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Ron Miles ronald.miles@gmail.com 413.329.4277 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060401/dd175268/attachment.html
John Butler
2006-Apr-01 14:06 UTC
[Rails] Re: Re: You have a nil object when you didn''t expect it!
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. -- Posted via http://www.ruby-forum.com/.
Rimantas Liubertas
2006-Apr-01 14:13 UTC
[Rails] Re: You have a nil object when you didn''t expect it!
<...>> <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 Butler
2006-Apr-01 14:22 UTC
[Rails] Re: Re: You have a nil object when you didn''t expect it!
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. -- Posted via http://www.ruby-forum.com/.
John Butler
2006-Apr-01 14:23 UTC
[Rails] Re: Re: You have a nil object when you didn''t expect it!
<% for comment in @post.comments -%> <p><% comment.body %><p> <% end %> -- Posted via http://www.ruby-forum.com/.
John Butler
2006-Apr-01 14:35 UTC
[Rails] Re: Re: You have a nil object when you didn''t expect it!
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! -- Posted via http://www.ruby-forum.com/.
John Butler
2006-Apr-01 14:42 UTC
[Rails] Re: Re: You have a nil object when you didn''t expect it!
To move the comments to a new directory just simply use <%= render :partial => "comment/comment", :collection => @post.comments %> -- Posted via http://www.ruby-forum.com/.
Ron Miles
2006-Apr-01 15:34 UTC
[Rails] Re: Re: You have a nil object when you didn''t expect it!
No problem. Glad I could help. On 4/1/06, John Butler <JohnnyButler7@gmail.com> wrote:> > 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! > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Ron Miles ronald.miles@gmail.com 413.329.4277 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060401/0b2d7946/attachment.html