I’m having a bit of a noob issue. I wanted to get devise to redirect to the last page the user visited. So I did the following…
def after_sign_in_path_for(resource)
request.referer
end
Works great…except if the user is actually logging in through the original form which causes a redirect loop.
I tried
def after_sign_in_path_for(resource)
if (request.referer == "/users/sign_in")
:pages_home
else
request.referer
end
end
But thats not working, most likely because I have no idea what request.referer is actually returning when it encounters the original user login page (www.example.com/users/sign_in).
Any ideas?
tldr; Using devise, I want to redirect to the page logged in from (i.e /blog/4) unless the page is /users/sign_in
SOLVED:
Matchu was right. The request.referer was returning the domain as well…
http://example.com/users/sign_in
(note: no www prefix)
I’m still interested in an alternative to request.referer if its an insecure or inefficient way.
Don’t redirect to referrers – it’s generally a bad idea.
Instead, pass a
nextvalue across in the query-string or form-data. Perhaps use something like:When a user tries to visit a page requiring authentication (e.g.,
/admin/posts/3/edit) the authenticationbefore_filterissues aredirect_to new_session_url(:next => request.path). Then code up the login action and view to preserve the:nextquery-string parameter.