简体   繁体   中英

Rails - Devise - Redirect to current page after login or logout

I am working on a Ruby on Rails application and using Devise for user authentication. However, whenever I login or logout (from any page) I am always redirected to the home page "/". While what I'm trying to do is that when I login from any page (eg "/events") I am redirected back to this exact page ("/events" in this case) after login. And the same goes for logout.

I tried following this link . But the output of store_user_location is always "/" and also, the functions after_sign_out_path_for and after_sign_out_path_for are not overridden and my code for them was never reached.

I added after_sign_out_path_for also in application_controller as follows:

def after_sign_in_path(resource_or_scope)
    puts stored_location_for(resource_or_scope) || super
end

How can I fix this problem?

Try using request.referrer to the sessions_controller.rb or application_controller.rb

You can access referer path using request.referer

Like below

protected

def after_sign_in_path_for(resource_or_scope)
  stored_location_for(resource_or_scope) || request.referrer
end

def after_sign_out_path_for(resource_or_scope)
  stored_location_for(resource_or_scope) || request.referrer
end

See the devise wiki

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM