undefined method `each’ for nil:NilClass… why?

In your posts controller, you need to define @posts, which, based on the error you haven’t.

# app/controllers/posts_controller.rb
class PostsController < ApplicationController
  def index
    @posts = Post.all
  end
end 

As @posts is not defined calling each on it will generate undefined methodeach’ for nil:NilClass`.

Leave a Comment