Ruby on rails 路由错误-未初始化常量

Ruby on rails 路由错误-未初始化常量,ruby-on-rails,ruby,rails-routing,Ruby On Rails,Ruby,Rails Routing,我无法在Rails 3.2.12中修复此问题,可能我遗漏了一些东西 config/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' =&

我无法在Rails 3.2.12中修复此问题,可能我遗漏了一些东西

config/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'
class AuthenticationsController < ApplicationController
  ...
end
class Authentication < ActiveRecord::Base
  ...
end
app/controllers/authentication\u controller.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'
class AuthenticationsController < ApplicationController
  ...
end
class Authentication < ActiveRecord::Base
  ...
end
类身份验证控制器
app/models/authentication.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'
class AuthenticationsController < ApplicationController
  ...
end
class Authentication < ActiveRecord::Base
  ...
end
类身份验证
我认为它应该与我目前的知识,但有一些我错过了

我的问题是,请告诉我出了什么问题

rouning错误

未初始化常量身份验证控制器


这是一条显示在
http://localhost:3000/auth/facebook/signout

Rails要求文件名与类名匹配。因此,您应该将
app/controllers/authentication\u controller.rb
重命名为
app/controllers/authentications\u controller.rb

虽然这个问题已经得到了回答,但我发现了另一个出现此错误的案例,并希望将其记录在这里供后代参考

如果在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
现在,将/app/views/foobars移动到/app/views/super/foobars 并且,将/app/controllers/foobars\u controller.rb移动到/app/controllers/super/foobars\u controller.rb 确保foobars_controller.rb位于超级模块中:

class Super::FoobarsController < ApplicationController
class Super::foobarscocontroller
现在转到您的.dev.server/foobars/ 您应该得到以下错误: 路由错误未初始化常量FoobarsController

现在,从routes.rb的开头删除资源:foobars 现在应该可以了


我花了一段时间才弄明白为什么会出现这个错误,我没有意识到生成脚手架会在routes中添加一个条目。rb

虽然它没有回答您的特定问题,但我在routes中收到了以下失败消息。rb

resources :republishes  do
    post '/attempt_all', :to => 'republishes/#attempt_all' . . .
我换成了

resources :republishes  do
    post '/attempt_all', :to => 'republishes#attempt_all' . . .

删除斜杠修复了我的问题。

在我的例子中,由于我构建了模块,它已经为控制器启动了路由,我定义了两次。因此,通过删除其中一个重复的资源路由,解决了我的问题。

请确保已为相关控制器创建了模型。

哦。谢谢你,阿方索。我以这种方式创建了控制器,所以文件本身被命名为
authentication\u controller.rb,也许我更改了类名。非常感谢您回答这样一个大多数人都知道的问题。@吊柱提示:在生成控制器时,您应该始终使用复数。谢谢!一直在寻找这个问题的答案太久了。如果您想在完整的上下文中查看源代码,那么它是源代码的git中心吗