1

I am using Devise with multiple models (three to be exact) and each role has some different interactions. For example, after the user model signs up I override a devise method to redirect them to a specific welcome path, where with the employer model I take them to a credit card form, etc.

As a result, I need to namespace everything. Namespacing the views and controllers are not tough, however, I was wondering if there is a way to namespace the controllers without having to specify EVERY devise controller.

For example, is there a way to basically do this:

devise_for :employers, :controller => "employers"

Instead of having to do this:

devise_for :employers, :controllers => { 
 :registrations => "employers/registrations",
 :sessions => "employers/sessions",
 :confirmations => "employers/confirmations",
 :passwords => "employers/passwords",
 :unlocks => "employers/unlocks",
 :mailer => "employers/mailer"
}

Might seem trivial but when you have three models to maintain it could get a bit much.

JoshL
  • 1,397
  • 1
  • 12
  • 28
  • Hi Josh, I have commented Marc answer below as I am in the same situation as you were back in 2012. I have 2 models and need now to set a third model inside a namespace (namely 'admin'). I was wondering if this was a good idea to keep this model (devise controllers and views associated) outside the namespace and control access to the namespace through classic authing... It avoids the headache of scoping the Devise stuff.. – Maxence Oct 26 '17 at 10:41

2 Answers2

2

Take a look at the following answer from Devise within namespace. Simply namespacing in the routes.rb will not produce the desired results. You'll have to generate controllers for each action you want. For sessions for example, You will have to create a new controller called Sessions in the controller Employer namespace:

bundle exec rails g controller employer/sessions

then subclass the new session controller from devise session controller to bring in all the Devise methods required to properly handle sessions:

class Employer::SessionsController < Devise::SessionsController
end

and change your 'devise_for :employers, :controller => "employers"' line in config/routes.rb to the following:

devise_for :employers, :controllers => { :sessions => "employer/sessions" }

Finally, as an optional step, you can generate views to app/views/employer/sessions directory. You can do this my setting "config.scoped_views = true" inside config/initializers/devise.rb and running the following to generate views scoped to employers:

rails generate devise:views users

This should generate templates at app/views/employer/sessions/new. Otherwise, the new session controller will just use the default view templates.

Hope this helps!

Community
  • 1
  • 1
Marc
  • 4,546
  • 2
  • 29
  • 45
  • Hi Marc, thanks for your answer. I am in the same situation as Josh. I have two models which are not namespaced at the moment (users and professionals). And it works perfectly fine.. though I want to implement a third model 'admin'. I have been thinking of namespacing devise views and controllers for this model but I realize it may be useless.. do you think I can keep generated admin controllers and views at base level, still having an 'admin' namespace, and checking if admin user is logged in at all inherited controllers of admin namespace ? I don't see anything preventing it but maybe I am w – Maxence Oct 26 '17 at 00:57
0

Will it work by just saying: devise_for :employers, :path => "employers" ?

Abram
  • 39,950
  • 26
  • 134
  • 184
  • No that wont work. That will simply add the '/employers' path prefix to the path. Also, doing something like ```namespace: employers do ...``` or ```scope :employers do ...``` doesnt work either. – JoshL Sep 01 '12 at 11:26
  • You might have to ask the devs involved with the devise project. – Abram Sep 02 '12 at 00:32