索引方法"undefined method `model_name' for NilClass:Class"出错



我在索引方法的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'

,这个代码是:

在rooms_controller.rb

 class RoomsController < ApplicationController
  respond_to :js, :html, :json
    def index
        @rooms = Room.all
    end

in model "room.rb"

class Room < ActiveRecord::Base
  attr_accessible :capacity, :description, :is_active, :room_name
  belongs_to :roometable, polymorphic: true
  has_many :events
end
在视图

>房间> 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

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

请帮助我,谢谢你的时间看这个

看起来您的roomnil在这个代码:

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

你忘记添加迭代器了吗?

<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 %>

相关内容

最新更新