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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:14:54+00:00 2026-06-11T10:14:54+00:00

I’m working towards generating a list into my view. When attempting to loop through

  • 0

I’m working towards generating a list into my view. When attempting to loop through my @residents instance variable, I receive the error

NoMethodError in Admins#show

Showing /Users/myCPU/rails_projects/whizcharts/app/views/residents/index.html.erb where line #3 raised:

undefined method `each' for nil:NilClass
Extracted source (around line #3):

1: <h1>Resident Profiles</h1>
2: <ul class="list-residents">
3:  <% @residents.each do |r| %>
4:      <li>
5:          <%= link_to r.fname, r %>
6:      </li>
Trace of template inclusion: app/views/admins/show.html.erb

I’ve tried the following…

  • Ran rake db:migrate in case that was the issue.

  • In my ‘/controllers/residents_controller’ file, I’ve tried changing

    @residents = Resident.find(:all) to @residents = Resident.find.all

Here is my code…

‘controllers/residents_controller.rb’

class ResidentsController < ApplicationController
  def index
    @residents = Resident.find(:all) 
  end

  def show
    @resident = Resident.find(params[:id])
  end

  def new
    @resident = Resident.new
  end

  def create
    @resident = Resident.new(params[:resident])
    if @resident.save 
      redirect_to @resident, :success => "Your submission was a success"
    else 
      render :action => 'new'
    end
  end

  def edit
    @resident = Resident.find(params[:id])
  end

  def update
    @resdient = Resident.find(params[:id])
    if @resident.update_attributes(params{:resident})
        flash[:success] = "Resident's profile updated"
        sign_in @resident
        redirect_to @resident
    else
      render 'edit'
    end
  end

  def destroy
    Resident.find(params[:id]).destroy
    flash[:success] = "Resident deleted"
    redirect_to residents_path
  end

  def _form
    @residents = Resident.paginate(page: params[:page])
  end
end

‘config/routes.rb’

Whizcharts::Application.routes.draw do
  root to: 'static_pages#home'
  resources :admins, :residents
  resources :sessions, only: [:new, :create, :destroy]

  # static_page 
  match '/help',       to: 'static_pages#help'
  match '/about' ,     to: 'static_pages#about'
  match '/contact',    to: 'static_pages#contact'

  # sign in | sign up
  match '/signup',    to: 'admins#new'
  match '/signin',    to: 'sessions#new'
  match '/signout',   to: 'sessions#destroy', via: :delete
 .
 .
 .
   match ':controller(/:action(/:id))(.:format)'
end

‘views/residents/index.html.erb’

<h1>Resident Profiles</h1>
<ul class="list-residents">
    <% @residents.each do |r| %>
        <li>
            <%= r.fname %>
        </li>
    <% end %>
</ul>

‘/db/schema.rb’

ActiveRecord::Schema.define(:version => 20120722195705) do

  create_table "admins", :force => true do |t|
    t.string   "fname"
    t.string   "lname"
    t.string   "soc"
    t.string   "dob"
    t.string   "gender"
    t.text     "address"
    t.string   "city"
    t.string   "state"
    t.string   "zip"
    t.string   "email"
    t.string   "phone1"
    t.string   "phone2"
    t.datetime "created_at",                               :null => false
    t.datetime "updated_at",                               :null => false
    t.string   "password_digest"
    t.string   "password"
    t.string   "password_confirmation"
    t.string   "remember_token"
    t.boolean  "super",                 :default => false
  end

  add_index "admins", ["email"], :name => "index_admins_on_email", :unique => true
  add_index "admins", ["remember_token"], :name => "index_admins_on_remember_token"

  create_table "residents", :force => true do |t|
    t.string   "fname"
    t.string   "lname"
    t.string   "dob"
    t.string   "gender"
    t.string   "soc"
    t.string   "address"
    t.string   "city"
    t.string   "state"
    t.string   "zip"
    t.string   "phone"
    t.string   "doc_fname"
    t.string   "doc_lname"
    t.string   "doc_phone1"
    t.string   "doc_phone2"
    t.string   "doc_fax"
    t.string   "doc_email"
    t.string   "guard_fname"
    t.string   "guard_lname"
    t.string   "guard_address"
    t.string   "guard_city"
    t.string   "guard_state"
    t.string   "guard_zip"
    t.string   "guard_phone1"
    t.string   "guard_phone2"
    t.string   "guard_email"
    t.datetime "created_at",    :null => false
    t.datetime "updated_at",    :null => false
  end

end

And since the NoMethodError is in Admins#show here’s the code for that.

‘/views/admins/show.html.erb’

<% provide(:title, @admin.fname + " " + @admin.lname) %>
<div class="row">
    <aside class="span4">
        <section>
            <h1>
                <%= gravatar_for @admin %>
                <%= @admin.fname + " " + @admin.lname %> 
            </h1>
        </section>
        <section class="resident">
            <div class="content">
                <div id="new-resident">
                    <%= link_to 'create a new resident', new_resident_path %>
                </div>
                <div id="list-residents">
                    <%= render :template => 'residents/index' %>
                </div>
            </div><!-- END content CLASS -->
        </section>
    </aside>
</div>

Finally, when I remove the each method from my ‘views/residents/index.html.erb’ file, my browser renders properly. I don’t feel as though I have an understanding of how to mix partials from different controllers nor how to properly use instance variables. Any help on this problem as well as providing the topic(s) I should be reading up on (in addition to the rails documentation) are appreciated.

  • 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-11T10:14:56+00:00Added an answer on June 11, 2026 at 10:14 am

    It appears you run into the show action in the Admins controller, not the index action (NoMethodError in Admins#show).

    Does this still happen if you add @residents = Resident.find(:all) under def show?

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I am currently running into a problem where an element is coming back from
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.