2013-04-06 41 views
26

我無法修復這個在Rails 3.2.12中,也許我錯過了一些東西。路由錯誤 - 未初始化的常量

配置/ routes.rb中

get "home/index" 
root :to => "home#index" 
devise_for :users, :only => :omniauth_callbacks 
match 'users/auth/:provider/callback' => 'authentications#create' 
match '/auth/:provider/signout' => 'authentications#signout' 

應用程序/控制器/ authentication_controller.rb

class AuthenticationsController < ApplicationController 
    ... 
end 

應用/模型/ authentication.rb

class Authentication < ActiveRecord::Base 
    ... 
end 

我認爲它應該與我目前的知識一起工作,但有一些我錯過了。

我的親切的問題是告訴什麼是錯的,請。

Rounting錯誤

uninitialized constant AuthenticationsController

這是一個消息,在http://localhost:3000/auth/facebook/signout

回答

43

Rails的顯示了需要的文件名以匹配類名。因此,您應該將app/controllers/authentication_controller.rb重命名爲app/controllers/authentications_controller.rb

+2

哦。謝謝@alfonso。我以這種方式創建了控制器'rails g controller authentication',因此文件本身被命名爲'authentication_controller.rb',也許我更改了類名稱。非常感謝您回答這樣一個衆所周知的問題。 – Davit 2013-04-06 00:20:09

+1

@Davit提示:在生成控制器時,您應始終使用複數形式。 – Bonifacio2 2014-03-13 18:08:05

4

雖然這個問題已經得到解答,但我發現了另一個我得到這個錯誤的案例,並且希望將它記錄在這裏作爲後代。

如果在routes.rb文件中定義了兩個類似的路由而沒有相應的控制器,則會得到未初始化的常量錯誤。

重現步驟:

rails generate scaffold foobar name:string 
bundle exec rake db:migrate 

添加資源:foobars到的routes.rb到一個新的作用域(注:腳手架生成過程中foobars資源已經被自動添加到您的routes.rb頂部)是這樣的:

resources :foobars 

    ######################################## 
    # SUPER 
    ######################################## 

    constraints host: ENV['SUPER_HOST'] do 
    scope module: :super do 
     resources :foobars 
     get '/' => 'super#index' 

    end 
    end 

現在,移動/應用/視圖/ foobars/應用/視圖/超/ foobars 和移動/app/controllers/foobars_controller.rb/app/controllers/super/foobars_controller.rb 確保foobars_controller.rb是超級模塊:

class Super::FoobarsController < ApplicationController 

現在去你的.dev.server/foobars/ 你應該得到這個錯誤: 路由錯誤未初始化的常量FoobarsController現在

,刪除資源:從routes.rb中 開始foobars它應該 在工作,在忙!

我花了一段時間來弄清楚爲什麼我得到這個錯誤,我並沒有意識到發生支架添加一個條目在routes.rb中

+0

https://github.com/swilson223/ParkingAppDevelopment如果你想在完整的環境中看到它,那麼就是源代碼的git集線器 – 2015-12-12 23:50:00

相關問題