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

Ruby on-RailsRubyUrl

Ruby on-Rails Problem Overview


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

The request.request_uri only returns the relative URL.

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

For Rails 3.2 or Rails 4+

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

For 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.

Solution 2 - Ruby on-Rails

I think that the Ruby on Rails 3.0 method is now request.fullpath.

Solution 3 - Ruby on-Rails

You could use url_for(only_path: false)

Solution 4 - Ruby on-Rails

DEPRECATION WARNING: Using #request_uri is deprecated. Use fullpath instead.

Solution 5 - Ruby on-Rails

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

Solution 6 - Ruby on-Rails

You can add this current_url method in the ApplicationController to return the current URL and allow merging in other parameters

# https://x.com/y/1?page=1 
# + current_url( :page => 3 )
# = https://x.com/y/1?page=3
def current_url(overwrite={})
	url_for :only_path => false, :params => params.merge(overwrite)
end

Example Usage:

current_url --> http://...
current_url(:page=>4) --> http://...&page=4

Solution 7 - Ruby on-Rails

For Ruby on Rails 3:

request.url
request.host_with_port

I fired up a debugger session and queried the request object:

request.public_methods

Solution 8 - Ruby on-Rails

In Ruby on Rails 3.1.0.rc4:

 request.fullpath

Solution 9 - Ruby on-Rails

I needed the application URL but with the subdirectory. I used:

root_url(:only_path => false)

Solution 10 - Ruby on-Rails

 url_for(params)

And you can easily add some new parameter:

url_for(params.merge(:tag => "lol"))

Solution 11 - Ruby on-Rails

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!

Solution 12 - Ruby on-Rails

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

Using #request_uri is deprecated. Use fullpath instead.

Solution 13 - Ruby on-Rails

Using Ruby 1.9.3-p194 and Ruby on Rails 3.2.6:

If request.fullpath doesn't work for you, try request.env["HTTP_REFERER"]

Here's my story below.

I got similar problem with detecting current URL (which is shown in address bar for user in her browser) for cumulative pages which combines information from different controllers, for example, http://localhost:3002/users/1/history/issues.

The user can switch to different lists of types of issues. All those lists are loaded via Ajax from different controllers/partials (without reloading).

The problem was to set the correct path for the back button in each item of the list so the back button could work correctly both in its own page and in the cumulative page history.

In case I use request.fullpath, it returns the path of last JavaScript request which is definitely not the URL I'm looking for.

So I used request.env["HTTP_REFERER"] which stores the URL of the last reloaded request.

Here's an excerpt from the partial to make a decision

- if request.env["HTTP_REFERER"].to_s.scan("history").length > 0
  - back_url = user_history_issue_path(@user, list: "needed_type")
- else
  - back_url = user_needed_type_issue_path(@user)
- remote ||= false
=link_to t("static.back"), back_url, :remote => remote

Solution 14 - Ruby on-Rails

This works for Ruby on Rails 3.0 and should be supported by most versions of Ruby on Rails:

request.env['REQUEST_URI']

Solution 15 - Ruby on-Rails

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

Solution 16 - Ruby on-Rails

If by relative, you mean just without the domain, then look into request.domain.

Solution 17 - Ruby on-Rails

You can use the ruby method:

:root_url

which will get the full path with base url:

localhost:3000/bla

Solution 18 - Ruby on-Rails

(url_for(:only_path => false) == "/" )? root_url : url_for(:only_path => false)

Solution 19 - Ruby on-Rails

In Rails 3 you can use

request.original_url

http://apidock.com/rails/v3.2.8/ActionDispatch/Request/original_url

Solution 20 - Ruby on-Rails

you can use any one for rails 3.2:

request.original_url
or
request.env["HTTP_REFERER"]
or
request.env['REQUEST_URI']

I think it will work every where

"#{request.protocol}#{request.host}:#{request.port}#{request.fullpath}"

Solution 21 - Ruby on-Rails

Rails 4.0

you can use request.original_url, output will be as given below example

get "/articles?page=2"

request.original_url # => "http://www.example.com/articles?page=2"

Solution 22 - Ruby on-Rails

You can either use

request.original_url 

or

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

to get the current URL.

Solution 23 - Ruby on-Rails

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

Solution 24 - Ruby on-Rails

if you want to be specific, meaning, you know the path you need:

link_to current_path(@resource, :only_path => false), current_path(@resource)

Solution 25 - Ruby on-Rails

For rails 3 :

> request.fullpath

Solution 26 - Ruby on-Rails

request.env["REQUEST_URI"]

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

Solution 27 - Ruby on-Rails

To get the request URL without any query parameters.

def current_url_without_parameters
  request.base_url + request.path
end

Solution 28 - Ruby on-Rails

You can set a variable to URI.parse(current_url), I don't see this proposal here yet and it works for me.

Solution 29 - Ruby on-Rails

You can use:

request.full_path

or

request.url

Hopefully it will resolve your problem.

Cheers

Solution 30 - Ruby on-Rails

To get the absolute URL which means that the from the root it can be displayed like this

<%= link_to 'Edit', edit_user_url(user) %>

The users_url helper generates a URL that includes the protocol and host name. The users_path helper generates only the path portion.

users_url: http://localhost/users
users_path: /users

Solution 31 - Ruby on-Rails

Rails 4

Controller:

def absolute_url
  request.base_url + request.original_fullpath
end

Action Mailer Notable changes in 4.2 release:

link_to and url_for generate absolute URLs by default in templates, it is no longer needed to pass only_path: false. (Commit)

View:

If you use the _url suffix, the generated URL is absolute. Use _path to get a relative URL.

<%= link_to "Home", root_url %>

For More Details, go to:

http://blog.grepruby.com/2015/04/absolute-url-full-url-in-rails-4.html

Solution 32 - Ruby on-Rails

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

Solution 33 - Ruby on-Rails

you can get absolute url by calling:

request.original_url

or

request.env['HTTP_REFERER']

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJakub ArnoldView Question on Stackoverflow
Solution 1 - Ruby on-RailsJaime BellmyerView Answer on Stackoverflow
Solution 2 - Ruby on-RailsmcrView Answer on Stackoverflow
Solution 3 - Ruby on-RailsgrzuyView Answer on Stackoverflow
Solution 4 - Ruby on-RailsecoologicView Answer on Stackoverflow
Solution 5 - Ruby on-RailsMikeView Answer on Stackoverflow
Solution 6 - Ruby on-RailsgrosserView Answer on Stackoverflow
Solution 7 - Ruby on-RailsDukeView Answer on Stackoverflow
Solution 8 - Ruby on-RailsLucas RenanView Answer on Stackoverflow
Solution 9 - Ruby on-RailsDorianView Answer on Stackoverflow
Solution 10 - Ruby on-RailsYuri BarbashovView Answer on Stackoverflow
Solution 11 - Ruby on-RailsJames MView Answer on Stackoverflow
Solution 12 - Ruby on-RailsKen EarleyView Answer on Stackoverflow
Solution 13 - Ruby on-RailsSergiy SeletskyyView Answer on Stackoverflow
Solution 14 - Ruby on-RailsTim SantefordView Answer on Stackoverflow
Solution 15 - Ruby on-RailsFransView Answer on Stackoverflow
Solution 16 - Ruby on-RailsghoppeView Answer on Stackoverflow
Solution 17 - Ruby on-RailsIdan WenderView Answer on Stackoverflow
Solution 18 - Ruby on-RailsmsrootView Answer on Stackoverflow
Solution 19 - Ruby on-RailsmikrobiView Answer on Stackoverflow
Solution 20 - Ruby on-RailsumaView Answer on Stackoverflow
Solution 21 - Ruby on-RailsArvind singhView Answer on Stackoverflow
Solution 22 - Ruby on-RailsRobin GargView Answer on Stackoverflow
Solution 23 - Ruby on-RailsAmitView Answer on Stackoverflow
Solution 24 - Ruby on-RailsVictor SView Answer on Stackoverflow
Solution 25 - Ruby on-RailsPankhuriView Answer on Stackoverflow
Solution 26 - Ruby on-RailsSatishakumar AwatiView Answer on Stackoverflow
Solution 27 - Ruby on-RailsNerveView Answer on Stackoverflow
Solution 28 - Ruby on-RailsRaK427View Answer on Stackoverflow
Solution 29 - Ruby on-RailsAli Hassan MirzaView Answer on Stackoverflow
Solution 30 - Ruby on-RailsAhmad hamzaView Answer on Stackoverflow
Solution 31 - Ruby on-Railsuser3118220View Answer on Stackoverflow
Solution 32 - Ruby on-RailsV K SinghView Answer on Stackoverflow
Solution 33 - Ruby on-Railsuser3355933View Answer on Stackoverflow