I have two kinds of users: Admin and Worker.
each user has role_ids: if user.role_ids = [1], the user is Admin. if user.role_ids = [2], the user is Worker.
After the sign_up/sign_in, I want to redirect him by his role_ids:
if his kind is Admin, redirect him to: localhost:3000/tasksadmins
.
if his kind is Worker, redirect him to: localhost:3000/workers
.
this is my routes.rb
:
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
resources :tasksadmins
resources :workers
root to: "workers#index"
I think I have to do something like:
if current_user.role_ids == [2]
root to: "workers#index"
else
root to: "tasksadmins#index"
end
but current_user is not defined in the routes.
Any help appreciated!
You can override Devise's RegistrationsController and modify the method after_sign_in_path_for
according to your needs:
def after_sign_in_path_for(resource_or_scope)
if resource_or_scope.is_a?(User)
return admins_path if resource_or_scope.admin?
workers_path
else
super
end
end
Hope this helps!
Try this:
Add a single root in routes.rb and in controller we have to mention where we go..!
In routes.rb :
root to: "taskadmins#index"
In taskadmins controller you can check with current_user
In taskadmins Controller.rb :
def index
if current_user.role_ids == [2]
redirect_to "/workers/index"
else
redirect_to "tasksadmins#index"
end
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.