简体   繁体   English

Rails - 获取没有GET参数的当前URL

[英]Rails - Get the current url without the GET parameters

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 ,还是我必须解析字符串以去除get参数?

request.path should return what you're looking for if you're not concerned about the hostname. 如果您不关心主机名, request.path应该返回您要查找的内容。 Otherwise you might try: 否则你可能会尝试:

url_for(:only_path => false, :overwrite_params=>nil)

To get the request URL without any query parameters. 获取没有任何查询参数的请求URL。

def current_url_without_parameters
  request.base_url + request.path
end

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,减去查询参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM