1

私は自分のビューにリストを生成することに取り組んでいます。@residents インスタンス変数をループしようとすると、エラーが発生します

管理者の NoMethodError#show

3 行目が発生した /Users/myCPU/rails_projects/whizcharts/app/views/residents/index.html.erb を表示:

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

私は次のことを試しました...

  • それが問題だった場合は、 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

「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

「ビュー/居住者/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 メソッドを削除すると、ブラウザーは適切にレンダリングされます。異なるコントローラーのパーシャルを混合する方法や、インスタンス変数を適切に使用する方法を理解しているとは思えません。この問題に関するヘルプと、(レールのドキュメントに加えて) 私が読むべきトピックを提供していただければ幸いです。

4

1 に答える 1

0

index アクション ( NoMethodError in Admins#show) ではなく、Admins コントローラの show アクションに遭遇したようです。

@residents = Resident.find(:all)の下に追加すると、これはまだ起こりますdef showか?

于 2012-09-16T23:22:47.380 に答える