Rails - Get the current url without the GET parame

2019-02-04 02:15发布

request.url returns me this : 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?

3条回答
Summer. ? 凉城
2楼-- · 2019-02-04 02:58

To get the request URL without any query parameters.

def current_url_without_parameters
  request.base_url + request.path
end
查看更多
Ridiculous、
3楼-- · 2019-02-04 03:00

request.path should return what you're looking for if you're not concerned about the hostname. Otherwise you might try:

url_for(:only_path => false, :overwrite_params=>nil)
查看更多
Root(大扎)
4楼-- · 2019-02-04 03:04

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.

查看更多
登录 后发表回答