33

In a Rails Model, I want to be able to find out the host and port. For example, if I am in a test environment it would return http://localhost:3000/ and if I was in production it would return something like http://my.application.com/?

Luccas
  • 4,078
  • 6
  • 42
  • 72
Jonathan
  • 16,077
  • 12
  • 67
  • 106
  • duplicate: http://stackoverflow.com/questions/42566/getting-the-hostname-or-ip-in-ruby-on-rails – ghoppe May 11 '10 at 18:19
  • possible duplicate of [How to find the local port a rails instance is running on?](http://stackoverflow.com/questions/1554267/how-to-find-the-local-port-a-rails-instance-is-running-on) for the port, http://stackoverflow.com/questions/42566/getting-the-hostname-or-ip-in-ruby-on-rails for the host. – Ciro Santilli OurBigBook.com Oct 09 '14 at 20:29

7 Answers7

31
"#{root_url}"

which should be declared in your routes.rb

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
Kamilski81
  • 14,409
  • 33
  • 108
  • 161
  • this is what I was looking for. request.host_with_port was working for me at first, but I needed the leading `HTTP://`. this works just like any other url / path helper. – Dudo Oct 08 '13 at 18:27
  • I think is answer is much better then the rest.. No request inspection, just plain router helper method – Yo Ludke Jul 03 '15 at 12:49
  • Good, but hacky and dangerous. Assumes root path is "/", and could introduce bugs if someone changes root path to ie "/Home" and does not update relevant code... – Andre Figueiredo Apr 19 '19 at 18:51
  • @AndreFigueiredo How so? The root path or url is set in `routes.rb` so if you were to change the root path, it would be changed in `routes.rb` and `root_url` would reflect that. If you're not updating the `routes.rb` file when making a change to your root path, you've got bigger problems to deal with. – Joshua Pinter Sep 14 '20 at 15:53
  • Not sure what you mean, but my point is that is not good to assume that host+port is and will always be the same as root_url. If you want host and port, you don't want something that looks like host and port right now, you actually fetch the host and port. yiwen answer is the correct one – Andre Figueiredo Sep 14 '20 at 17:57
  • thanks, dude!! it was a nightmare to get the hostname! I'm amazed that such simple thing is not easily possible in rails URL helpers! – Khaled AbuShqear Jun 10 '21 at 09:31
25

host_with_port should work for you: http://apidock.com/rails/ActionDispatch/Http/URL/host_with_port

yiwen
  • 1,057
  • 1
  • 12
  • 17
20

You can use this

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

When you use in string concatenation

"#{request.protocol}#{request.host_with_port}/book/1"
# https://example.com:3000/book/1
Pagorn
  • 532
  • 1
  • 5
  • 25
Rakesh
  • 284
  • 2
  • 5
7

You can get those values in your controllers (request.host, request.port etc.).

You'd have to give that to your models via parameters as the request object is only available in the controllers.

Maxem
  • 2,634
  • 1
  • 20
  • 14
2

Other answers provided work in controllers or views but can't provide host name in eg: messaging jobs. Action mailer has it's own configuration for this that you're likely using anyway.

# Access host from anywhere 
Rails.configuration.action_mailer.default_url_options[:host]

# Defined in development.rb/test.rb/production.rb
config.action_mailer.default_url_options = { host: 'www.example.com' }
pdenya
  • 445
  • 5
  • 12
0

you can get full uri (http://localhost:3000/ or http://my.application.com/ etc) with request_uri

http://apidock.com/rails/ActionController/AbstractRequest/request_uri

fl00r
  • 82,987
  • 33
  • 217
  • 237
-2

Request.host

http://api.rubyonrails.org/classes/ActionController/Request.html#M000712

ghoppe
  • 21,452
  • 3
  • 30
  • 21