在我的视图实例变量上使用每个方法返回NoMethodError,未定义的.each方法

时间:2012-09-16 23:05:21

标签: ruby-on-rails ruby-on-rails-3.2

我正在努力为我的视图生成一个列表。当试图遍历我的@residents实例变量时,我收到错误

管理员#show中的NoMethodError

显示/Users/myCPU/rails_projects/whizcharts/app/views/residents/index.html.erb第3行提出:

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

我尝试了以下内容......

  • Ran rake db:migrate,以防出现问题。

  • 在我的'/ controllers / resident_controller'文件中,我尝试过更改

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

这是我的代码......

'控制器/ 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

'配置/ 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

的观点/居民/ index.html.erb'

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

'/分贝/ 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

由于NoMethodError位于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>

最后,当我从'views / resident / index.html.erb'文件中删除每个方法时,我的浏览器会正确呈现。我不觉得我对如何混合来自不同控制器的部分以及如何正确使用实例变量有所了解。对此问题的任何帮助以及提供我应该阅读的主题(除了rails文档)都表示赞赏。

1 个答案:

答案 0 :(得分:0)

您似乎遇到了Admins控制器中的show动作,而不是索引操作(NoMethodError in Admins#show)。

如果您在@residents = Resident.find(:all)下添加def show

,是否还会发生这种情况