How can I get the current absolute URL in my Ruby on Rails view?
The request.request_uri
only returns the relative URL
It looks like request_uri
is deprecated in Ruby on Rails 3.
Using #request_uri is deprecated. Use fullpath instead.
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
(url_for(:only_path => false) == "/" )? root_url : url_for(:only_path => false)
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
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
request.env["REQUEST_URI"]
works in rails 2.3.4 tested and do not know about other versions.