I have a page whose path is (e.g.) /premises/92 on which I’m displaying “please [log in] or [register] for additional information” if the user is not logged in, and I want devise to return that same /premises/92 page after the user logs in.
I’ve read other posts and I think I understand how devise’s stored_location_for is supposed to work. In theory, I could put something like this in my ApplicationController:
def stored_location_for(resource)
if (r = session[:return_to])
session[:return_to] = nil
r
else
super
end
end
My question is: how / where do I set up session[:return_to]?
I want to set session[:return_to] only if the user clicks on [log in] or [register], but what’s the best way to do that?
- Decorate the links with JavaScript? That could work, but seems heavy-handed.
- Set it in the Premises Controller before rendering the page? That doesn’t seem right: what if the user doesn’t click on the [log in] or [register] links? Then I have session[:return_to] set to some odd value which might trip me up if the user logs in from some other page.
- Add a
?return_to=/premises/92query string to the [log in] and [register] links, and detect that in the RegistrationsController and SessionsController and use that info to set up session[:return_to]? That seems like it would work, but also heavy-handed.
None of these smell right. What’s the generally accepted technique for setting up state for stored_location_for?
Devise use
So you can use
session["user_return_to"]if your model for authentication is User.