Created
July 13, 2011 14:09
-
-
Save skojin/1080353 to your computer and use it in GitHub Desktop.
replacement for redirect_to(:back) that works even if no referer (e.g. via direct url)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# same as redirect_to(:back) but works even if no referer (e.g. via direct url) | |
# include this module to ApplicationController | |
module BackDefaultRedirect | |
protected | |
# use as redirect_to back_or_default(root_url) | |
def back_or_default(default = '/') | |
referer = request.env['HTTP_REFERER'] | |
# if has HTTP_REFERER and it not equals current url | |
if referer && !referer.include?(request.request_uri) | |
:back | |
else | |
default | |
end | |
end | |
def redirect_to_back(default = '/') | |
redirect_to back_or_default(default) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment