Skip to content

Instantly share code, notes, and snippets.

@skojin
Created July 13, 2011 14:09
Show Gist options
  • Save skojin/1080353 to your computer and use it in GitHub Desktop.
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)
# 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