I am using both Cancan and Devise. I have the following so when cancan errors redirects the page to sign in path:
check_authorization :unless => :devise_controller?
rescue_from CanCan::AccessDenied do |exception|
redirect_to new_user_session_path, :alert => exception.message
end
That works well. However, I want to be able to redirect back to that Cancan protected page once user has signed in.
I use the script in the Devise wiki here (How To: redirect to a specific page on successful sign in).
def after_sign_in_path_for(resource)
sign_in_url = url_for(:action => 'new', :controller => 'sessions', :only_path => false, :protocol => 'http')
if request.referer == sign_in_url
# this path is used
super
else
stored_location_for(resource) || request.referer || root_path
end
end
But this will always use the super
method, which goes to index page. What have I done wrong?
I know it's late, but I solved it by adding session["user_return_to"] to rescue_from
check_authorization :unless => :devise_controller?
rescue_from CanCan::AccessDenied do |exception|
session["user_return_to"] = request.fullpath
redirect_to new_user_session_path, :alert => exception.message
end
and removing the after_sign_in_path_for method override. Then Devise will redirect to session["user_return_to"] after sign in.