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
How to Replace Accented Latin Characters in Ruby
How Does Instance_Eval Work and Why Does Dhh Hate It
Ruby Multiline Block Without Do End
How to Update Ruby Version 2.0.0 to the Latest Version in MAC Osx Yosemite
Ruby: Kind_Of? Vs. Instance_Of? Vs. Is_A
How to Specify a Local Gem in My Gemfile
Why Does Installing Nokogiri on MAC Os Fail With Libiconv Is Missing
Pass Variables to Ruby Script Via Command Line
What's the Difference Between a Proc and a Lambda in Ruby
Continuously Read from Stdout of External Process in Ruby
Why Does Ruby Have Both Private and Protected Methods