How can I get the current absolute URL in my Ruby on Rails view?
The request.request_uri only returns the relative URL.
How can I get the current absolute URL in my Ruby on Rails view?
The request.request_uri only returns the relative URL.
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
end
You can write "#{request.protocol}#{request.host_with_port}#{request.fullpath}", since request.url is now deprecated.
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.
url_for(params)
And you can easily add some new parameter:
url_for(params.merge(:tag => "lol"))
I think request.domain would work, but what if you're in a sub directory like blah.blah.com? Something like this could work:
<%= request.env["HTTP_HOST"] + page = "/" + request.path_parameters['controller'] + "/" + request.path_parameters['action'] %>
Change the parameters based on your path structure.
Hope that helps!
This works for Ruby on Rails 3.0 and should be supported by most versions of Ruby on Rails:
request.env['REQUEST_URI']
For Rails 3.x and up:
#{request.protocol}#{request.host_with_port}#{request.fullpath}
For Rails 3.2 and up:
request.original_url
Because in rails 3.2 and up:
request.original_url = request.base_url + request.original_fullpath
For more info, plese visit http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-original_url