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.

有帮助吗?

解决方案 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.

其他提示

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 %>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top