devise overriding registrations controller - uninitialized constant Users::RegistrationsController
Asked Answered
S

6

16

I'm trying to override some the functionality of the default devise registrations controller so that only certain users can create accounts for others. So in a file called registrations_controller.rb under the controllers/users folder I have the following

class Users::RegistrationsController < Devise::RegistrationsController

  before_filter :check_permissions, :only => [:new, :create, :cancel]
  skip_before_filter :require_no_authentication

  def check_permissions
    authorize! :create, resource
  end
end

and in my routes file I have

devise_for :users, :controllers => { :registrations => 'users/registrations' }

When I try to go to the users/sign_up url I get a a routing error 'uninitialized constant Users::RegistrationsController'.

So what is really weirding me out about this is that I had used pretty much exactly the same functionality in a rails 3 app without a problem. I had a look at some of the other stackoveflow questions similar to this and I'm still none the wiser. The app I'm building now is a rails 3.1 app and I'm using devise 1.5.1

Here are the relevant routes it case they useful

new_user_session GET    /users/sign_in(.:format)                                      {:action=>"new", :controller=>"devise/sessions"}
                         user_session POST   /users/sign_in(.:format)                                    {:action=>"create", :controller=>"devise/sessions"}
                 destroy_user_session DELETE /users/sign_out(.:format)                                   {:action=>"destroy", :controller=>"devise/sessions"}
                        user_password POST   /users/password(.:format)                                   {:action=>"create", :controller=>"devise/passwords"}
                    new_user_password GET    /users/password/new(.:format)                               {:action=>"new", :controller=>"devise/passwords"}
                   edit_user_password GET    /users/password/edit(.:format)                              {:action=>"edit", :controller=>"devise/passwords"}
                                      PUT    /users/password(.:format)                                   {:action=>"update", :controller=>"devise/passwords"}
             cancel_user_registration GET    /users/cancel(.:format)                                     {:action=>"cancel", :controller=>"users/registrations"}
                    user_registration POST   /users(.:format)                                            {:action=>"create", :controller=>"users/registrations"}
                new_user_registration GET    /users/sign_up(.:format)                                    {:action=>"new", :controller=>"users/registrations"}
               edit_user_registration GET    /users/edit(.:format)                                       {:action=>"edit", :controller=>"users/registrations"}
                                      PUT    /users(.:format)                                            {:action=>"update", :controller=>"users/registrations"}
                                      DELETE /users(.:format)                                            {:action=>"destroy", :controller=>"users/registrations"}
Sewoll answered 11/12, 2011 at 19:33 Comment(0)
C
16

I would say, there's something wrong at your filename.

Your file should be called users/registrations_controller.rb

That works for me.

Consentient answered 16/4, 2013 at 20:11 Comment(0)
S
5

Where was your registrations_controller.rb saved to? The location is important. I found that I was making a mistake saving it to app/controllers/devise/.. It simply needed to be saved in app/controllers/. e.g.:

app/controllers/registrations_controller.rb


Also, config/routes.rb route should be defined as:

devise_for :users, controllers: { registrations: 'registrations' }

Scholl answered 5/7, 2014 at 17:38 Comment(0)
G
2

I tried the same setup that you put here but it worked for me. I uploaded the application to github (I uploaded the log as well, so you can see that it really worked).

Double check for possible typos. Maybe you forgot a plural or there is a typo on a Class name.

Glaucous answered 13/12, 2011 at 0:10 Comment(2)
Thanks for sticking that example up. It is really strange, I seem to have exactly the same set up as you but I'm still getting that uninitialized constant error. I'll keep looking for typosSewoll
I had the same problem, turned out to be a typo for me as well.Luger
E
0

Hi I just recently added first name and last name to my registration. I am using Rails 4.

I used the following instructions/tutorial to get this done:

http://www.jacopretorius.net/2014/03/adding-custom-fields-to-your-devise-user-model-in-rails-4.html.

:)

Eustache answered 4/11, 2014 at 5:55 Comment(0)
S
0

If you has been generate the views Move view files

I assume you already use rails generate devise:views generated devise views. Move views/devise/registrations folder to views/users , I think you also you should change the _path in the forms

Superficies answered 21/8, 2017 at 14:33 Comment(0)
H
0

Do rails routes and check your routes in config/routes you might have a typo in your routes.

Instead of registrationS#new you might have something else like registration#new.

Hysteroid answered 29/4, 2018 at 22:9 Comment(1)
check devise_scope :user in config/routes.rbHysteroid

© 2022 - 2024 — McMap. All rights reserved.