undefined method `each' for nil:NilClass... why?
Asked Answered
B

4

19

rails guide example, click the button save post, console show this message:

Started POST "/posts" for 127001 at 2013-12-25 22:42:04 +0800 Processing by PostsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"CLalUww3gqnSlED0AWdou6P/U2qya vPqDiBANQOuYgA=", "post"=>{"title"=>"11", "text"=>"22"}, "commit"=>"Save Post"} (0.0ms) begin transaction (0.0ms) rollback transaction Redirected to http:// 127001:3000/posts Completed 302 Found in 16ms (ActiveRecord: 0.0ms)

Started GET "/posts" for 127001 at 2013-12-25 22:42:04 +0800 Processing by PostsController#index as HTML Rendered posts/index.html.erb within layouts/application (15.6ms) Completed 500 Internal Server Error in 31ms

ActionView::Template::Error (undefined method `each' for nil:NilClass):

        <th>Text</th>
        </tr>
        <% @posts.each do |post| %>

======================================================

routes is correct, why post is nil? rails 4.0.2 ruby 2.0

Bohannon answered 25/12, 2013 at 15:14 Comment(2)
Please show the code of PostsController#index.Pishogue
thanks i know the reason, i used post ,should be posts.but now new issue raise undefined method `title' for nil:NilClassBohannon
G
35

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`.

Genniegennifer answered 25/12, 2013 at 15:17 Comment(2)
def index @post = Post.all end the code exist should be post or posts?Bohannon
@user3134762, notice that you have singular @post in your index action but you are using plural @posts in your view.Genniegennifer
C
13

To explain more about this error whenerver you encounter

  undefined method `each' for nil:NilClass

The error clearly complaints that you're calling each method on something here(@posts) which is nil. That means you've not defined it in your controller. Since you didn't define it, that's why it is complaining undefined method for nil class.

Please makes sure to check whenever you call an instance variable from your view? you must have to define that in your controller to be accessible in views.

Sometimes you'll also get this error if you call a private method in your controller.

Coomer answered 13/12, 2014 at 9:4 Comment(0)
E
3

Try replacing this:

<% @posts.each do |post| %>

with

<% Post.all.each do |post| %>
Eyeleteer answered 4/7, 2020 at 20:2 Comment(3)
Hi @anosha-rehan , Can you expand this?Chiro
Hi @Jeremas, I'm sorry but I unfortunately don't have this particular project anymoreEyeleteer
@anosha-rehan , No problem, Thanks!Chiro
P
0

In my case, this worked... <% Article.all.each do |post| %>

However I did want use the variable "@article", defined at controller as:

class PstArticlesController < ApplicationController def index @articles = Article.all end def show @article = Article.find(params[:id]) end end Thanks!

Pawl answered 27/4, 2024 at 1:38 Comment(1)
It is the same as https://mcmap.net/q/630470/-undefined-method-each-39-for-nil-nilclass-whyBelford

© 2022 - 2025 — McMap. All rights reserved.