0

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

 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

在模型“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 %>

在路线.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

我尝试解决此问题,但没有发生任何问题。

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

4

2 回答 2

1

看起来你roomnil这段代码中:

content_tag_for :div , room, class: 'row-fluid'
于 2013-01-14T08:06:01.393 回答
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 %>
于 2013-01-14T08:24:28.793 回答