1

我正在努力在我的视图中生成一个列表。尝试循环通过我的 @residents 实例变量时,我收到错误

管理员中的 NoMethodError#show

显示 /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/residents_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

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

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

由于 NoMethodError 在 Admins#show 中,因此这里是它的代码。

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

最后,当我从“views/residents/index.html.erb”文件中删除 each 方法时,我的浏览器会正确呈现。我不觉得我了解如何混合来自不同控制器的部分,也不了解如何正确使用实例变量。感谢您提供有关此问题的任何帮助以及提供我应该阅读的主题(除了 rails 文档)。

4

1 回答 1

0

看来您在 Admins 控制器中遇到了 show 操作,而不是 index 操作 ( NoMethodError in Admins#show)。

如果添加@residents = Resident.find(:all)under还会发生这种情况def show吗?

于 2012-09-16T23:22:47.380 回答