How do I get the current absolute URL in Ruby on Rails?

后端 未结 30 2106
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 13:47

How can I get the current absolute URL in my Ruby on Rails view?

The request.request_uri only returns the relative URL

相关标签:
30条回答
  • 2020-11-22 14:02

    It looks like request_uri is deprecated in Ruby on Rails 3.

    Using #request_uri is deprecated. Use fullpath instead.
    
    0 讨论(0)
  • 2020-11-22 14:03

    If you're using Rails 3.2 or Rails 4 you should use request.original_url to get the current URL.


    Documentation for the method is at http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-original_url but if you're curious the implementation is:

    def original_url
      base_url + original_fullpath
    end
    
    0 讨论(0)
  • 2020-11-22 14:08
    (url_for(:only_path => false) == "/" )? root_url : url_for(:only_path => false)
    
    0 讨论(0)
  • 2020-11-22 14:10

    For Rails 3.2 or Rails 4 Simply get in this way "request.original_url" Reference: Original URL Method

    For Rails 3 As request.url is deprecated.We can get absolute path by concatenating

    "#{request.protocol}#{request.host_with_port}#{request.fullpath}"
    

    For Rails 2

    request.url
    
    0 讨论(0)
  • 2020-11-22 14:11

    None of the suggestions here in the thread helped me sadly, except the one where someone said he used the debugger to find what he looked for.

    I've created some custom error pages instead of the standard 404 and 500, but request.url ended in /404 instead of the expected /non-existing-mumbo-jumbo.

    What I needed to use was

    request.original_url
    
    0 讨论(0)
  • 2020-11-22 14:13
    request.env["REQUEST_URI"]
    

    works in rails 2.3.4 tested and do not know about other versions.

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