Skip to content

Instantly share code, notes, and snippets.

@kossnocorp
Created March 1, 2014 21:38
Show Gist options
  • Save kossnocorp/9297810 to your computer and use it in GitHub Desktop.
Save kossnocorp/9297810 to your computer and use it in GitHub Desktop.
def back_or_home_path
referrer = controller.respond_to?(:request) && controller.request.env['HTTP_REFERER']
local_referrer = referrer && URI.parse(referrer).host == controller.request.host
if referrer && local_referrer
referrer
else
root_path
end
end
@vrybas
Copy link

vrybas commented Mar 2, 2014

def back_or_home_path
  controller.respond_to?(:request) && referer_is_local ? referer : root_path
end

private
  def referer_is_local
    URI.parse(referer).host == controller.request.host
  end

  def referer
    @referer ||= controller.request.env['HTTP_REFERER']
  end

@rwz
Copy link

rwz commented Mar 2, 2014

There is a request.referrer method

@mkaschenko
Copy link

http://stackoverflow.com/a/8136648

URI(request.referer).host == request.host

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment