Daniel
Daniel

Reputation: 3040

Override Devise Registrations Controller Two Times?

I have two models, User and Admin. I want to know if its possible to override Devise's Registrations Controller and have two custom Registrations Controllers - one for each model.

I know its possible to get what I want by overriding the registrations controller and I would just use If else statements although (correct me if I am wrong) I believe its better to avoid having many if else statements if possible.

You can see what I've done so far on another post - I have scoped views and it uses the wrong set of views for some reason. Devise Views with Multiple Models

Upvotes: 1

Views: 2113

Answers (1)

Richard Peck
Richard Peck

Reputation: 76774

Yes it's possible


Routes

When you generate Devise for both models, you'll have to add it to your routes:

#config/routes.rb
devise_for :users
devise_for :admin

Devise actually uses these routes to populate its arguments, one of which is the controllers argument:

#config/routes.rb
devise_for :users, controllers: { sessions: "sessions", registrations: "registrations" }
devise_for :admin, controllers: { registrations: "admin/session" }

Controllers

This will allow you to create controllers to override the Devise default ones:

#app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
    #Your Code Here
end

#app/controllers/admin/registrations_controller.rb
class Admin::RegistrationsController < Devise::RegistrationsController
    #Your Code Here
end

Upvotes: 7

Related Questions