0

I want to set after sign in root path in devise this is my code in application controller

def signed_in_root_path(scope_or_resource)
if current_user.role == "dealer"
  dashboard_dealer_path
elsif current_user.role == "admin"
  admin_dashboard_path
else
  dashboard_customer_path
end
end

After sign in i should never be able to return to root page which is in routes.rb file

devise_scope :user do
  root :to => 'carinfos#index'
end

after sign in iam able to go to carinfos/index page(i.e, i should not see localhost:3000), when i go to that page i should be redirected to dealer dashboard if logged as dealer

1 Answer 1

1

In devise session controller there is a method called after_sign_in_path_for

def after_sign_in_path_for(resource)
    if resource.role == "dealer"
      dashboard_dealer_path 
    elsif resource.role == "admin"
      admin_dashboard_path
   else
     dashboard_customer_path
   end
end

just override session controller

class SessionsController < Devise::SessionsController
6
  • I tried it, and when i type localhost:3000 it is not redirecting me to localhost:3000/dashboard/dealer.and i think we can override in application controller itself right? Nov 8, 2013 at 16:39
  • After sigin in it redirect to dashboard properly, if i type localhost:3000 it should redirect to dashboard if signed in so tried signed_in_root_path Nov 8, 2013 at 16:46
  • in your application controller...check if user is not signed in redirect to sign_in path and when user signed_in then redirect to appropriate path
    – LHH
    Nov 8, 2013 at 17:17
  • after user sign in he is redirected properly, but after sign in he should not be able to see the landing page/root page mentioned in routes Nov 8, 2013 at 17:39
  • if user is dealer then redirect to dealer path and if admin then redirect to admin path.... u want to redirect in this manner right?
    – LHH
    Nov 8, 2013 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.