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 5989801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:09:12+00:00 2026-05-22T23:09:12+00:00

When i access http://localhost:3000/users , it gives me NoMethodError in Users#index . The error

  • 0

When i access http://localhost:3000/users, it gives me NoMethodError in Users#index. The error is as follows:

NoMethodError in Users#index

Showing /Applications/XAMPP/xamppfiles/htdocs/rails_projects/TUTORIALS/todo/app/views/users/index.html.erb where line #2 raised:

undefined method `name' for nil:NilClass
Extracted source (around line #2):

1: <% @users.each do |user| %>
2:   hello!! <%  @user.name %>
3: <% end %>
Rails.root: /Applications/XAMPP/xamppfiles/htdocs/rails_projects/TUTORIALS/todo

Application Trace | Framework Trace | Full Trace
app/views/users/index.html.erb:2:in `block in _app_views_users_index_html_erb___3451413751309771876_2169723860_4451603425222664605'
app/views/users/index.html.erb:1:in `each'
app/views/users/index.html.erb:1:in `_app_views_users_index_html_erb___3451413751309771876_2169723860_4451603425222664605'

My model user.rb:

# Table name: users
#
#  id         :integer         not null, primary key
#  name       :string(255)
#  email      :string(255)
#  created_at :datetime
#  updated_at :datetime
#

class User < ActiveRecord::Base
  attr_accessible :name, :email

My view app/views/users/index.html.erb:

<% @users.each do |user| %>
  hello!! <%  @user.name %>
<% end %>

My controller app/controllers/users_controller.rb

class UsersController < ApplicationController

  def index
   @users = User.all  
  end

My routes.rb file has:

resources :users

All my tests pass (using rspec) including the test case in spec/controllers/users_controller_spec.rb:

describe "GET 'index'" do
     it "should be successful" do
       get 'index'
       response.should be_success
     end
end

When I access http://localhost:3000/users/1, it shows me the users perfectly. Codes in app/views/users/show.html.erb:

<p>
  <b>Name:</b>
  <%= @user.name %>
</p>

I have done rake db:test:prepare and I think the error is linked to the db or migrations. Any idea? Thanks!

  • 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-05-22T23:09:13+00:00Added an answer on May 22, 2026 at 11:09 pm

    This is incorrect:

    <% @users.each do |user| %>
      hello!! <%  @user.name %>
    <% end %>
    

    It should be:

    <% @users.each do |user| %>
      hello!! <%= user.name %>
    <% end %>
    

    In your code the object @user does not exist, that’s why you get the error. In the iteration each user in @users is put into the user object one-by-one.

    Another mistake in your code is that you were using <% @user.name %>. This does not output anything. If you want to output the name of the user you have to use <%= user.name %> (notice the equal sign).

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

Sidebar

Related Questions

When I access http://localhost/myapp111/index/ then this script will excute: $a = 0; for($i =
response = Typhoeus::Request.get(http://localhost:3000/api/api_email/#{@api_id}.json) JSON.parse(response.body) The response is a JSON object but I get an
I have simple Spring MVC application. When I try to access http://localhost:8080/paygate/transaction I get
I folllowed this : http://ruby.railstutorial.org/chapters/filling-in-the-layout#sec:user_signup . But when I try to access http://localhost:3000/pages/ it
This doesn't seem to work: $.ajax({ url: http://localhost:3000/foo.json, data: { foo: 'bar' }, headers:
So here's what I'd like to do: access to http://example.com/ * would require the
Can I access spring bean that exposed using http invoker (server) from GWT application
How do I access a page's HTTP response headers via JavaScript? Related to this
I found this via google: http://www.mvps.org/access/api/api0008.htm '******************** Code Start ************************** ' This code was
Is there a way to access the response headers from an HTTP result when

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.