Ruby-on-rails 路由错误 - 未初始化的常量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15845491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Routing error - uninitialized constant
提问by Davit
I could not fix this in Rails 3.2.12, maybe I am missing something.
我无法在 Rails 3.2.12 中解决这个问题,也许我遗漏了一些东西。
config/routes.rb
配置/路由.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'
app/controllers/authentication_controller.rb
应用程序/控制器/authentication_controller.rb
class AuthenticationsController < ApplicationController
...
end
app/models/authentication.rb
应用程序/模型/authentication.rb
class Authentication < ActiveRecord::Base
...
end
I think it should work with my current knowledge, but there is something that I miss.
我认为它应该适用于我目前的知识,但我想念一些东西。
My kind question would be to tell what is wrong, please.
我的问题是请告诉我出了什么问题。
Rounting Error
循环错误
uninitialized constant AuthenticationsController
uninitialized constant AuthenticationsController
This is a message that shows up at http://localhost:3000/auth/facebook/signout
这是一条消息,显示在 http://localhost:3000/auth/facebook/signout
回答by alf
Rails requires the file name to match the class name. Therefore you should rename app/controllers/authentication_controller.rbto app/controllers/authentications_controller.rb.
Rails 要求文件名与类名匹配。因此,您应该重命名app/controllers/authentication_controller.rb为app/controllers/authentications_controller.rb.
回答by Jared Menard
Though this question has been answered, I found another case where I was getting this error and wanted to document it here for posterity.
虽然这个问题已经得到回答,但我发现了另一个案例,我遇到了这个错误,并想在这里记录它以供后代使用。
If you have two similar routes defined in your routes.rb file without the corresponding controllers you will get the uninitialized constant error.
如果您在 routes.rb 文件中定义了两个相似的路由,而没有相应的控制器,您将收到未初始化的常量错误。
Steps to reproduce:
重现步骤:
rails generate scaffold foobar name:string
bundle exec rake db:migrate
add resources :foobarsto routes.rb to a new scope (note: the foobars resource was already automatically added to the top of your routes.rb during scaffold generation) like this:
将资源 :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
Now, move /app/views/foobarsto /app/views/super/foobarsand, move /app/controllers/foobars_controller.rbto /app/controllers/super/foobars_controller.rbMake sure foobars_controller.rb is in the Super module:
现在,将/app/views/foobars移至/app/views/super/foobars并将/app/controllers/foobars_controller.rb移至/app/controllers/super/foobars_controller.rb确保 foobars_controller.rb 位于 Super 模块中:
class Super::FoobarsController < ApplicationController
Now go to your.dev.server/foobars/ You should get this error: Routing Error uninitialized constant FoobarsController
现在转到 your.dev.server/foobars/ 你应该得到这个错误:Routing Error uninitialized constant FoobarsController
Now, remove resources :foobarsfrom beginning of routes.rb It should work now!
现在,从routes.rb的开头删除资源 :foobars 它现在应该可以工作了!
It took me awhile to figure out why I was getting this error, and I didn't realize that generating the scaffold adds an entry in routes.rb
我花了一段时间才弄清楚为什么我会收到这个错误,我没有意识到生成脚手架会在 routes.rb 中添加一个条目
回答by NorseGaud
While it doesn't answer your specific question, I received the failure with the following in my routes.rb
虽然它没有回答您的具体问题,但我在 routes.rb 中收到了以下失败信息
resources :republishes do
post '/attempt_all', :to => 'republishes/#attempt_all' . . .
which I changed to
我改成
resources :republishes do
post '/attempt_all', :to => 'republishes#attempt_all' . . .
Removing the slash fixed my issue.
删除斜线解决了我的问题。
回答by Tushar
In my case, Since I'd scaffold the module, it was already had initiated routes for the controller and I was defining it twice. So by removing one of the duplicate resource routes resolved my issue.
就我而言,由于我为模块搭建了脚手架,因此它已经为控制器启动了路由,并且我定义了两次。因此,通过删除重复的资源路由之一解决了我的问题。
回答by David Adafia
make sure you've created your model for the controller in question.
确保您已经为相关控制器创建了模型。

