Question

In my controller I do:

@categories = Category.all
for i in @categories.min.id..@categories.max.id
   @allposts.push(Post.where(:Category => i))
end

In views(haml) i do

<% @allposts.each do |posts, slots| %>
  <% @slots.each do |post| %>
    <%= post.Title %>
  <% end %>
<% end %>

And i see this error:

undefined method `each' for nil:NilClass on @slots.each do |post|.

Thank you in advance.

Was it helpful?

Solution 2

Fix is :

<% @allposts.each do |posts, slots| %>
  <% slots.each do |post| %>  # <--- see I removed @ symbol
    <%= post.Title %>
  <% end %>
<% end %>

Your block variable is slots, and you attempted as @slots. Which I think is a typo. As there is no such @slots variable defined, you got nil, when you wanted to use it. There is a sweet difference between local variable and instance variable. If you don't defined anywhere a local variable say foo, you will be ended up with undefined local variable or method if you want to use foo anywhere else.But for instance variable, no error, you will silently get nil.

OTHER TIPS

You haven't defined @slots in the method.Perhaps you mean slots.

<% @allposts.each do |posts, slots| %>
  <% slots.each do |post| %>
    <%= post.Title %>
  <% end %>
<% end %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top