Set different root paths for each type of user

I created a custom model. There are 2 types of users:

  • client
  • admin

I created bij by creating two "normal" models: client and admin. These two models inherit from the user model, for example:

class Customer < User 

Does anyone know how I can configure the root path for each type of user. I want something like this:

 authenticated :customer do root :to => "customer/dashboard#index" end authenticated :admin do root :to => "admin/dashboard#index" end 

UPDATE:

I solved the problem:

 root :to => "pages#home", :constraints => lambda { |request|!request.env['warden'].user} root :to => 'customer/dashboard#index', :constraints => lambda { |request| request.env['warden'].user.type == 'customer' } root :to => 'admin/dashboard#index', :constraints => lambda { |request| request.env['warden'].user.type == 'admin' } 
+7
source share
3 answers

although the old question, there is no answer, and it may be useful to others.

In rails 3.2 (I never tested it with anything below) you can do this in the routes.rb file

 authenticated :admin_user do root :to => "admin_main#index" end 

then your normal root route will be further down.

This, however, does not seem to work in rails 4, since it gives Invalid route name, already in use: 'root' (ArgumentError) (as I just found out and looked for a solution when I came across this question), if I figure out a way make it on rails 4 I will update my answer

Edit:

Good, so for Rails 4, the fix is ​​pretty simple, but not so obvious from the start. all you have to do is make the second root route a named route by adding like: like this:

 authenticated :admin_user do root :to => "admin_main#index", as: :admin_root end 

this is documented here , but note that this only seems to be a temporary fix and therefore may be changed in the future

+6
source

What you can do is have one root path, say home#index and in the appropriate controller action, redirect depending on their user type.

For example:

 def index if signed_in? if current_user.is_a_customer? #redirect to customer root elsif current_user.is_a_admin? #redirect to admin root end end end 
+2
source

Using after_sign_in_path_for should be appropriate. So add this to your application_controller.rb :

 def after_sign_in_path_for(resource) if resource.type == 'customer' your_desired_customer_path else your_desired_admin_path end end 
0
source

All Articles