Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Rails, how to get current url (but no paths)

If I'm in a URL such as

http://domain.com/mysite/bla 

How can I request just the URL with no paths? Such as

http://domain.com  
like image 726
Martin Avatar asked Apr 07 '11 05:04

Martin


People also ask

What is the best way to get the current request URL in Rails?

You should use request. original_url to get the current URL.


2 Answers

request.host should do the trick, or:

request.port.blank? ? request.host : "#{request.host}: #{request.port}" 

if you need to include the port too.

like image 29
Pravin Avatar answered Sep 29 '22 05:09

Pravin


You can use this

<%= request.protocol + request.host_with_port %> #=> https://domain.com:3000 <%= request.protocol + request.host %> #=> https://domain.com 

Starting from Rails 3.2 you can also use

<%= request.base_url %> #=> https://domain.com:3000 
like image 150
fl00r Avatar answered Sep 29 '22 03:09

fl00r