Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

The request.request_uri only returns the relative URL.

like image 575
Jakub Arnold Avatar asked Jan 29 '10 22:01

Jakub Arnold


People also ask

Is Ruby on Rails a framework?

Ruby on Rails is known as an MVC (model-view-controller) full-stack framework. The code is separated into three interconnected layers: Model contains the logic of an application, all the essential data, and high-level classes.

How can you create a new Rails project?

Run RubyMine and click New Project on the Welcome Screen. Name: specify a name for the project (rails-helloworld in our case). Location: specify a path to the directory in which you want to create the project. By default, RubyMine creates a directory with the same name as the project.


1 Answers

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.

like image 54
15 revs, 15 users 33% Avatar answered Sep 27 '22 21:09

15 revs, 15 users 33%