Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8299813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:18:06+00:00 2026-06-08T16:18:06+00:00

Im following the RoR tutorial by Michael Hartl and im at chapter 9.3 Showing

  • 0

Im following the RoR tutorial by Michael Hartl and im at chapter 9.3 “Showing all users” so far everything has worked great but now I am getting an undefined method `each’ for nil:NilClass when trying to retrieve my users from the SQlite database. Here is my controller

class UsersController < ApplicationController

before_filter :signed_in_user, only: [:index, :edit, :update]
before_filter :correct_user,   only: [:edit, :update]

.
.
.

def index
@users = User.all
end
end

    and my index.html.erb

<ul class="users">

<% @users.each do |user| %>
<li>
<%= gravatar_for user, size: 52 %>
<%= link_to user.name, user %>
</li>
<% end %>
</ul>

With this code i get the error undefined method `each’ for nil:NilClass

And when i change it to

<ul class="users">
<% if @users %>
<% @users.each do |user| %>
<li>
<%= gravatar_for user, size: 52 %>
<%= link_to user.name, user %>
</li>
<% end %>
<% end %>
</ul>

I can render the view but displaying 0 users although i have users in my db. I have created some manually and also used the ‘faker’ gem to spawn some. In the rails console typing User.all returns an array with 100 users. I cant seem to find the missing link here. Im also using the SQlite Database browser app to check out my User model where i also have 100 Users.
I have worked a lot with this and cant seem to figure things out.

Here is my User.rb aswell

class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password

before_save { |user| user.email = email.downcase }
before_save :create_remember_token

validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence:   true,
format:     { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true

private

def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:18:10+00:00Added an answer on June 8, 2026 at 4:18 pm

    It’s curious that User.all is returning nil instead of an empty array []. Try the following:

    1. make sure you’ve run any pending migrations

      rake db:migrate
      
    2. If the previous step didn’t run any migrations, try wiping your database and starting again, just for good measure:

      rake db:drop
      rake db:create db:migrate db:seed
      
    3. go into a rails console, and make sure User.all is behaving correctly

      rails c
      >> User.all
        User Load (0.4ms)  SELECT "users".* FROM "users" 
      => []
      >> exit
      
    4. If this all works, try putting some debug statements into your controller (using pry or the ruby debugger is optimal, but even some puts statements will suffice here) to examine the value of @users

    5. Next, you’ll want to either add some seeds to the database (edit db/seeds.rb and run rake db:seed) or use scaffolded forms to add some users.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am completely new to ror and am following Michael Hartl's tutorial but I
I am following Michael Hartl's RoR tutorial, and it is covering the basics of
I am currently following Michael Hartl's tutorial on RoR and am trying to get
I thought I was following this RoR tutorial to a T, but apparently not.
I am following Michael's ROR tutorial and creating user authentication system. There is an
I'm trying to learn RoR following this tutorial and I'm currently in chapter 3.
Following ROR's guides getting started tutorial , I am making a blog having two
After following the RoR getting started tutorial, I added another model as: $ rails
I'm following the RoR 3 Rails Tutorials, but to upgrade to a newer version
I am trying an ror tutorial and I came across the following line of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.