Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在我的视图上使用each方法';s实例变量返回NoMethodError,未定义。每个方法_Ruby On Rails_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails 在我的视图上使用each方法';s实例变量返回NoMethodError,未定义。每个方法

Ruby on rails 在我的视图上使用each方法';s实例变量返回NoMethodError,未定义。每个方法,ruby-on-rails,ruby-on-rails-3.2,Ruby On Rails,Ruby On Rails 3.2,我正在努力在视图中生成一个列表。在尝试循环使用@residents实例变量时,我收到了错误消息 管理员秀中的命名错误 显示/Users/myCPU/rails#u projects/whizcharts/app/views/incidents/index.html.erb,其中第3行出现: undefined method `each' for nil:NilClass Extracted source (around line #3): 1: <h1>Resident Profi

我正在努力在视图中生成一个列表。在尝试循环使用@residents实例变量时,我收到了错误消息

管理员秀中的命名错误

显示/Users/myCPU/rails#u projects/whizcharts/app/views/incidents/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
“config/routes.rb” 'views/residents/index.html.erb' 既然命名者在Admins中,那么下面是代码

“/views/admins/show.html.erb”

'居民/索引'%>

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

似乎您在Admins控制器中遇到了show操作,而不是index操作(
Admins中的NoMethodError 35; show


如果添加
@residents=Resident,是否仍会发生这种情况。在
def show
下查找(:all)

@residents设置为nil。数据库中有居民吗?我已添加了1个居民,但尚未确认数据库是否接受。我做了一个快速搜索,以确定是否有rakedb命令用于在我的表中显示数据,但到目前为止没有找到任何结果。当我可以确认你的问题的答案时,我会再次回答。db正在保存配置文件,根据Nils Landt的建议,我的浏览器现在正在正确渲染。是的,当我将@residents=Resident.find(:all)添加到我的“controllers/admins\u controller.rb”文件时,我的浏览器正确渲染。早些时候我没有选择这个选项,因为我不知道它是否状态不佳。脱钩等等。这样做不是很糟糕吗?如果您不执行任何重定向,那么无论如何只会调用控制器中的一个方法,这意味着如果您希望在show操作和index操作中显示所有驻留,那么您需要在控制器中使用该特定代码行两次(或者将其放入helper方法中,并调用helper方法两次)。
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
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
<h1>Resident Profiles</h1>
<ul class="list-residents">
    <% @residents.each do |r| %>
        <li>
            <%= r.fname %>
        </li>
    <% end %>
</ul>
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
<% 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>