snippetrubyrailsCritical
How do I get the current absolute URL in Ruby on Rails?
Viewed 0 times
urlhowabsolutethecurrentrubyrailsget
Problem
How can I get the current absolute URL in my Ruby on Rails view?
The
The
request.request_uri only returns the relative URL.Solution
For Rails 3.2 or Rails 4+
You should use
This method is documented at original_url method, but if you're curious, the implementation is:
For Rails 3:
You can write
For Rails 2:
You can write
You should use
request.original_url to get the current URL. Source code on current repo found here.This method is documented at original_url method, but if you're curious, the implementation is:
def original_url
base_url + original_fullpath
endFor Rails 3:
You can write
"#{request.protocol}#{request.host_with_port}#{request.fullpath}", since request.url is now deprecated.For Rails 2:
You can write
request.url instead of request.request_uri. This combines the protocol (usually http://) with the host, and request_uri to give you the full address.Code Snippets
def original_url
base_url + original_fullpath
endContext
Stack Overflow Q#2165665, score: 1512
Revisions (0)
No revisions yet.