34

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"
devise_for :users, :only => :omniauth_callbacks
match 'users/auth/:provider/callback' => 'authentications#create'
match '/auth/:provider/signout' => 'authentications#signout'

app/controllers/authentication_controller.rb

class AuthenticationsController < ApplicationController
  ...
end

app/models/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

Davit
  • 1,340
  • 5
  • 20
  • 47

5 Answers5

51

Rails requires the file name to match the class name. Therefore you should rename app/controllers/authentication_controller.rb to app/controllers/authentications_controller.rb.

alf
  • 17,592
  • 10
  • 58
  • 90
  • 2
    oh. thank you @alfonso. I created controller in this way `rails g controller authentication` so file was named to `authentication_controller.rb` by itself, maybe I changed class name. Thank you very much for responding to such a known-by-most question. – Davit Apr 06 '13 at 00:20
  • 2
    @Davit Tip: You should always use plural while generating controllers. – Bonifacio2 Mar 13 '14 at 18:08
  • Thank you! Been searching for the answer to this one for far too long. – alex Aug 21 '18 at 12:13
5

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

Jared Menard
  • 2,300
  • 1
  • 17
  • 20
  • https://github.com/swilson223/ParkingAppDevelopment Is the git hub to the source code if you want to see it in full context – Shawn Wilson Dec 12 '15 at 23:50
0

While it doesn't answer your specific question, I received the failure with the following in my 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.

NorseGaud
  • 141
  • 8
0

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.

Tushar
  • 1
  • 2
-1

make sure you've created your model for the controller in question.

David Adafia
  • 221
  • 3
  • 9