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

时间:2022-07-18 10:42:42

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参数?

3 个解决方案

#1


43  

request.path should 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)

#2


8  

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.

它不会重新生成路径,因此会为您提供最终用户在浏览器中看到的网址,减去查询参数。

#3


8  

To get the request URL without any query parameters.

获取没有任何查询参数的请求URL。

def current_url_without_parameters
  request.base_url + request.path
end

#1


43  

request.path should 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)

#2


8  

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.

它不会重新生成路径,因此会为您提供最终用户在浏览器中看到的网址,减去查询参数。

#3


8  

To get the request URL without any query parameters.

获取没有任何查询参数的请求URL。

def current_url_without_parameters
  request.base_url + request.path
end