2013-04-06 46 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

相关问题