Last Updated: February 25, 2016
·
5.17K
· leemachin

Redirect to previous URL after login with Devise

Dumping users at a particular page once they've logged in, no matter what page they were attempting to visit, is a usability nightmare. Finding out how to implementing something you'd think Devise would cover is equally nightmarish.

You can send users back to the page they actually wanted to see quite easily when you're using Devise:

def after_sign_in_path_for(resource_or_scope)
  session.fetch 'user_return_to', user_path
end

You can make it a bit more magic when you're working with multiple scopes. A session key will exist for each one, eg. admin_return_to, other_scope_return_to, etc.