How to Get the Current Absolute Url in Ruby on Rails

How do I get the current absolute URL in 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.

Rails: How do I get the current full URL along with the tab?

You can't do it with Rails. Because the anchor is not sent to the server side.

In Rails you can only get URL with params.

But you can do it using JS: window.location

Ruby on Rails how to get current URL?

request.original_url 

shall provide you current url in rails 4.

You can visit this resource for more info @ How do I get the current absolute URL in Ruby on Rails?

How to get the full URL including hash in Rails/Ruby

Anchors are not being sent to the server so that's why you cannot get them.

How do I get the current absolute URL in 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.

Rails current url helper

request.original_url should work according to the documentation.

Returns the original request URL as a string

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

You could also try string concatenation with different variables.

request.host + request.full_path

If that doesn't work either, you could try

url_for(:only_path => false);

Relative to absolute url in rails

You just need to prepend "http://" to the url.

<%= link_to raw(truncate(strip_tags(record.sch.first.to_s + ': ' + record.name), :length => 120, :omission => "...")), ('http://' + Figaro.env.base_url + record.named_url) %>



Related Topics



Leave a reply



Submit