I use devise and tried to do the next thing:
when the user sign in/up, I want to redirect him by his role_id (I let id of 1 for some users and 2 for the others).
if his role_id is 1, redirect him to tasksadmins_path, otherwise to workers_path.
so I tried something like:
routes.rb:
devise_for :users, :controllers => { :sessions => 'user_sessions'} do
get '/users/sign_out' => 'devise/sessions#destroy'
root to: "workers#index"
end
resources :tasksadmins
resources :workers
root to: "workers#index"
and this is my application_controller.rb:
class ApplicationController < ActionController::Base
include ApplicationHelper
protect_from_forgery
before_filter :authenticate_user!
rescue_from CanCan::AccessDenied do |exception|
if current_user.role_ids == [2]
redirect_to root_url
else
redirect_to tasksadmins_path
end
end
end
the
after_sign_in_path_fordoesn’t work, so I added to ‘create’ the next lines:in the beginning, I wrote:
and then I wrote in the end of the ‘create’ function:
so my create looks so: