对于NilClass:Class,索引方法“未定义方法`model_name'有错误”

时间:2013-01-14 08:01:17

标签: html ruby-on-rails postgresql

我在索引方法的Room部分有一些错误,请参阅错误

 NoMethodError in Rooms#index

Showing /home/kingdark/Repos/glowfish/app/views/rooms/_room.html.erb where line #2 raised:

undefined method `model_name' for NilClass:Class

Extracted source (around line #2):

1: <h1>&nbsp;Rooms List <span class='pull-right'> <%= link_to "New Room", new_room_path, class: 'btn btn-primary' %> </span> </h1> 
2: <%= content_tag_for :div , room, class: 'row-fluid' do %> 
3: <div id="r-index_content"> 
4: <li> 
5:   <span class='span9' >

Trace of template inclusion: app/views/rooms/index.html.erb

Rails.root: /home/kingdark/Repos/glowfish Application Trace | Framework Trace | Full Trace

app/views/rooms/_room.html.erb:2:in `_app_views_rooms__room_html_erb__3875627950727331108_69928712927400' app/views/rooms/index.html.erb:1:in `_app_views_rooms_index_html_erb__2888541332730611846_47144000'

,此代码为:

在room_controller.rb中

 class RoomsController < ApplicationController
  respond_to :js, :html, :json

    def index
        @rooms = Room.all
    end

在模型“room.rb”

class Room < ActiveRecord::Base
  attr_accessible :capacity, :description, :is_active, :room_name

  belongs_to :roometable, polymorphic: true
  has_many :events
end

在views&gt; rooms&gt; index.html.erb

<h1>&nbsp;Rooms List <span class='pull-right'> <%= link_to "New Room", new_room_path, class: 'btn btn-primary' %> </span> </h1>
<%= content_tag_for :div , room, class: 'row-fluid' do %>
<div id="r-index_content">
<li>
  <span class='span9' >
    <%= link_to room.room_name, room ,{:style => 'color: #000000'} %> 
  </span>
  <span class='span3'>
    <span class='pull-right'>
      <%= link_to "Edit", edit_room_path(room), class: 'btn btn-mini' %> 
      <%= link_to "Delete", room_path(room), method: :delete, data: { confirm: "Are you sure to delete this room?" }, class: 'btn btn-mini btn-danger' %>
    </span>
  </span>
 </li>
</div>
<% end %>

在routes.rb

Glowfish::Application.routes.draw do
  match '/calendar(/:year(/:month))' => 'calendar#index', :as => :calendar, :constraints => {:year => /\d{4}/, :month => /\d{1,2}/}

  devise_for :users

  resources :pages
  resources :areas
  resources :rooms

  match '/:id' => 'high_voltage/calendar#index', :as => :static, :via => :get
  root :to => 'high_voltage/calendar#index', :id => 'index'
end

我试图解决这个问题,但没有任何事情发生,这是错误的。

请帮帮我,谢谢你有时间看到这个。

2 个答案:

答案 0 :(得分:1)

在此代码中看起来roomnil

content_tag_for :div , room, class: 'row-fluid'

答案 1 :(得分:0)

你有没有忘记添加迭代器?

<h1>&nbsp;Rooms List <span class='pull-right'> <%= link_to "New Room", new_room_path, class: 'btn btn-primary' %> </span> </h1>
<% @rooms.each do |room| %>
  <%= content_tag_for :div , room, class: 'row-fluid' do %>
  ...
<% end %>