控制器Rails中未初始化的常量

时间:2016-03-22 16:14:29

标签: ruby-on-rails ruby controller

这是路线:

Rails.application.routes.draw.do
devise_for :users do
collection do
  post 'batch_invite'
end
end
root 'welcome#index'

resources :rosters, only: [:index, :show]

end

我的名册控制员:

class RostersController < ApplicationController
 def index
  @rosters = Roster.all.order("created_at_DESC")
  end
 end

我的索引:

<div id="banner" class="roster_banner">
  <h1>View and create rosters</h1>
</div>

<div id="rosters" class="cf">
  <div class="wrapper">
    <ul class="cf">
      <% @rosters.each do |roster| %>
        <%= link_to roster do %>
          <li>

            <div class="roster_overview">
              <h2><%= roster.title %></h2>
              <p class="week_count">Week 1</p>
            </div>
          </li>
        <% end %>
      <% end %>
    </ul>

    <%= will_paginate @rosters, previous_label: "Previous", next_label: "Next" %>
  </div>
</div>

我无法看到任何错误,但当我加载localhost:3000 /名单时,我得到:

Error: NameError (uninitialized constant RostersController::Roster):
  app/controllers/rosters_controller.rb:3:in `index'

我得到的错误:未初始化的常量RostersController :: Roster 1

1 =错误的屏幕截图

3 个答案:

答案 0 :(得分:4)

  

未初始化的常量RostersController :: Roster

您应该在roster.rb

中有一个名为app/models的模型文件
#roster.rb

class Roster < ActiveRecord::Base
end

答案 1 :(得分:0)

Rails在命名方面非常有意义(出于很多好的理由)。你必须遵守这样的规则(注意案例和单数/复数形式):

File name            | Class name | Table name
app/models/roster.rb | Roster     | rosters

否则你会有错误。

答案 2 :(得分:0)

除了@ Pavan的回答,

从控制器索引操作的屏幕截图中,这将无效 @rosters = Roster.all.order("created_at_DESC")

试试这个而不是 @rosters = Roster.order('created_at': 'DESC')
@rosters = Roster.order("created_at DESC")

@rosters = Roster.order(created_at: :desc)

此外,all不需要order

http://apidock.com/rails/ActiveRecord/QueryMethods/order