Skip to content

Instantly share code, notes, and snippets.

@envygeeks
Last active June 8, 2019 02:58
Show Gist options
  • Save envygeeks/8252474 to your computer and use it in GitHub Desktop.
Save envygeeks/8252474 to your computer and use it in GitHub Desktop.
def clean_for_action_based_url(url)
CGI.unescape(url || "").
gsub(/\/+/, "/").
tr('^a-z_-/', "")
end
def clean_url(url)
url = CGI.unescape(url || "")
url = url.gsub(/\A((https?:)?\/+)*/, "").gsub(/\/+/, "/")
url.split("/").map { |v| CGI.escape(v) }.
join("/").prepend("/")
end
redirect_to clean_url(params[:next]) # Without Rails
redirect_to root_path(params[:next],
only_path: true
) # Rails
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment