Ruby-on-rails Rails - 获取不带 GET 参数的当前 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4947652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails - Get the current url without the GET parameters
提问by MrRuru
request.url returns me this : http://localhost:3000/page?foo=bar.
request.url 返回给我:http://localhost:3000/page?foo=bar。
Is there a method I can call to get http://localhost:3000/page, or do I have to parse the string to strip get parameters?
有没有我可以调用的方法来获取http://localhost:3000/page,或者我是否必须解析字符串以去除获取参数?
回答by tadman
request.pathshould return what you're looking for if you're not concerned about the hostname. Otherwise you might try:
request.path如果您不关心主机名,应该返回您要查找的内容。否则你可以尝试:
url_for(:only_path => false, :overwrite_params=>nil)
回答by Nerve
To get the request URL without any query parameters.
获取不带任何查询参数的请求 URL。
def current_url_without_parameters
request.base_url + request.path
end
回答by thekingoftruth
I use the following:
我使用以下内容:
request.original_url.split('?').first
It does not regenerate the path, and therefore gives you exactly the url that the end-user sees in their browser, minus query parameters.
它不会重新生成路径,因此为您提供最终用户在其浏览器中看到的 url,减去查询参数。

