In Rails 2.3, I could just use HOST to get my hostname but it seems it's not available anymore. What can I use instead?
I just want something that returns localhost:xxxx
(with real port number) when I run the code from my machine, and http://www.mysite.com
when I run it in production.
You have several options:
`hostname
`use the socket gem:
require 'socket'
Socket.gethostname
in case you just want to know the current domain the request visits: request.domain
Update
From the request object, we can fetch both the host and the port, please check out these methods:
request.protocol
request.host
request.port
You could build the full url from these methods.
From my personal experience, for most projects you might have a fixed domain for each environment, and usually configure that in a yaml file or so (for example, to send email and use urls with domain name in the email body, we usually read the config and set options for url helper). So I usually just read the current environment's urls configuration and use them from the code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With