简体   繁体   English

路由错误 - 未初始化的常量

[英]Routing error - uninitialized constant

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

This is a message that shows up at http://localhost:3000/auth/facebook/signout这是一条显示在http://localhost:3000/auth/facebook/signout

Rails requires the file name to match the class name. Rails 要求文件名与类名匹配。 Therefore you should rename app/controllers/authentication_controller.rb to app/controllers/authentications_controller.rb .因此,您应该将app/controllers/authentication_controller.rb重命名为app/controllers/authentications_controller.rb

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 :foobars to 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:resources :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/foobars to /app/views/super/foobars and, move /app/controllers/foobars_controller.rb to /app/controllers/super/foobars_controller.rb Make 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 :foobars from beginning of routes.rb It should work now!现在,从routes.rb的开头删除资源 : foob​​ars 它现在应该可以工作了!

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 中添加一个条目

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.删除斜线解决了我的问题。

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.因此,通过删除重复的资源路由之一解决了我的问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM