Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing the each method on my view's instance variable returns NoMethodError, undefinded .each method
    primarykey
    data
    text
    <p>I'm working towards generating a list into my view. When attempting to loop through my @residents instance variable, I receive the error </p> <p>NoMethodError in Admins#show</p> <p>Showing /Users/myCPU/rails_projects/whizcharts/app/views/residents/index.html.erb where line #3 raised:</p> <pre><code>undefined method `each' for nil:NilClass Extracted source (around line #3): 1: &lt;h1&gt;Resident Profiles&lt;/h1&gt; 2: &lt;ul class="list-residents"&gt; 3: &lt;% @residents.each do |r| %&gt; 4: &lt;li&gt; 5: &lt;%= link_to r.fname, r %&gt; 6: &lt;/li&gt; Trace of template inclusion: app/views/admins/show.html.erb </code></pre> <p>I've tried the following...</p> <ul> <li><p>Ran rake db:migrate in case that was the issue.</p></li> <li><p>In my '/controllers/residents_controller' file, I've tried changing </p> <p><code>@residents = Resident.find(:all) to @residents = Resident.find.all</code></p></li> </ul> <p>Here is my code...</p> <h1>'controllers/residents_controller.rb'</h1> <pre><code>class ResidentsController &lt; 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 =&gt; "Your submission was a success" else render :action =&gt; '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 </code></pre> <h1>'config/routes.rb'</h1> <pre><code>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 </code></pre> <h1>'views/residents/index.html.erb'</h1> <pre><code>&lt;h1&gt;Resident Profiles&lt;/h1&gt; &lt;ul class="list-residents"&gt; &lt;% @residents.each do |r| %&gt; &lt;li&gt; &lt;%= r.fname %&gt; &lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; </code></pre> <h1>'/db/schema.rb'</h1> <pre><code>ActiveRecord::Schema.define(:version =&gt; 20120722195705) do create_table "admins", :force =&gt; 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 =&gt; false t.datetime "updated_at", :null =&gt; false t.string "password_digest" t.string "password" t.string "password_confirmation" t.string "remember_token" t.boolean "super", :default =&gt; false end add_index "admins", ["email"], :name =&gt; "index_admins_on_email", :unique =&gt; true add_index "admins", ["remember_token"], :name =&gt; "index_admins_on_remember_token" create_table "residents", :force =&gt; 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 =&gt; false t.datetime "updated_at", :null =&gt; false end end </code></pre> <p>And since the NoMethodError is in Admins#show here's the code for that.</p> <h1>'/views/admins/show.html.erb'</h1> <pre><code>&lt;% provide(:title, @admin.fname + " " + @admin.lname) %&gt; &lt;div class="row"&gt; &lt;aside class="span4"&gt; &lt;section&gt; &lt;h1&gt; &lt;%= gravatar_for @admin %&gt; &lt;%= @admin.fname + " " + @admin.lname %&gt; &lt;/h1&gt; &lt;/section&gt; &lt;section class="resident"&gt; &lt;div class="content"&gt; &lt;div id="new-resident"&gt; &lt;%= link_to 'create a new resident', new_resident_path %&gt; &lt;/div&gt; &lt;div id="list-residents"&gt; &lt;%= render :template =&gt; 'residents/index' %&gt; &lt;/div&gt; &lt;/div&gt;&lt;!-- END content CLASS --&gt; &lt;/section&gt; &lt;/aside&gt; &lt;/div&gt; </code></pre> <p>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. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload