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

MrR*_*uru 31 url ruby-on-rails

request.url返回给我:http:// localhost:3000/page?foo = bar.

有没有我可以调用的方法来获取http:// localhost:3000/page,还是我必须解析字符串以去除get参数?

tad*_*man 48

request.path如果您不关心主机名,应该返回您要查找的内容.否则你可能会尝试:

url_for(:only_path => false, :overwrite_params=>nil)
Run Code Online (Sandbox Code Playgroud)


Ner*_*rve 11

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

def current_url_without_parameters
  request.base_url + request.path
end
Run Code Online (Sandbox Code Playgroud)


the*_*uth 9

我使用以下内容:

request.original_url.split('?').first
Run Code Online (Sandbox Code Playgroud)

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