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

后端 未结 30 2103
佛祖请我去吃肉
佛祖请我去吃肉 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: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
    

提交回复
热议问题