I’m developing a website. I want to show all the posts from all users. But I got the following error message:
undefined method `each’ for nil:NilClass
Extracted source (around line #3):
\app\view\static_pages\home.html.erb:
<div class="span8">
<ul class="microposts">
<% @microposts.each do |micropost| %>
<li>
micropost.content
</li>
and I’ve already defined in the
\app\controller\static_pages_controller.rb:
class StaticPagesController < ApplicationController
def home
if signed_in?
@micropost = current_user.microposts.build
@feed_items = current_user.feed.paginate(page: params[:page])
end
end
def index
@microposts = Micropost.all
end
end
What’s missing? I have 310 sample posts in the database already…
What page are you trying to show posts on? If that’s
home, then you should modify yourhomeaction to load all posts:Maybe it was the
@feed_itemsyou wanted to display on thehomepage?