Routing error - uninitialized constant

后端 未结 5 1766
-上瘾入骨i
-上瘾入骨i 2020-12-28 13:28

I could not fix this in Rails 3.2.12, maybe I am missing something.

config/routes.rb

get \"home/index\"
root :to => \"home#index\         


        
5条回答
  •  梦谈多话
    2020-12-28 13:40

    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.

    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
    
      ########################################
      # 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:

    class Super::FoobarsController < ApplicationController
    

    Now go to your.dev.server/foobars/ You should get this error: Routing Error uninitialized constant FoobarsController

    Now, remove resources :foobars from beginning of routes.rb It should work now!

    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

提交回复
热议问题