Rails - Get the current url without the GET parameters

后端 未结 3 1200
谎友^
谎友^ 2020-12-15 02:58

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 t

相关标签:
3条回答
  • 2020-12-15 03:28

    To get the request URL without any query parameters.

    def current_url_without_parameters
      request.base_url + request.path
    end
    
    0 讨论(0)
  • 2020-12-15 03:31

    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)
    
    0 讨论(0)
  • 2020-12-15 03:45

    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.

    0 讨论(0)
提交回复
热议问题