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! -- Posted via http://www.ruby-forum.com/.
John Butler 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 -- Posted via http://www.ruby-forum.com/.
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? -- Posted via http://www.ruby-forum.com/.
Because, as Dan mentioned, inside your partial, _categorylist.rhtml, the local variable is named after the partial. Use this instead: <% =categorylist.name %> jt On 4/4/06, John Butler <JohnnyButler7@gmail.com> wrote:> Thanks for yuor reply dan. > I have put the following code in _categorylist.rhtml > <b>right template</b> > <%= @category.name %>
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? -- Posted via http://www.ruby-forum.com/.
Anyone? -- Posted via http://www.ruby-forum.com/.
try this: def index @posts = Post.find(:all) @categories = Category.find(:all) end --r 2006/4/4, John Butler <JohnnyButler7@gmail.com>:> Anyone? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
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! -- Posted via http://www.ruby-forum.com/.
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. On 04/04/06, Robert Wagner <robbie.wilhelm@gmail.com> wrote:> > try this: > > def index > @posts = Post.find(:all) > @categories = Category.find(:all) > end > > --r > > 2006/4/4, John Butler <JohnnyButler7@gmail.com>: > > Anyone? > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060404/e17f520e/attachment.html