Set the Domain or Host in Rails URL Helpers
You can explicitly set the domain
with Rails URL Helpers:
app.root_url
=> "http://www.example.com/"
app.root_url(domain: 'www.customdomain.org')
=> "http://www.customdomain.org"
If we include the protocol in our domain argument, this brings up an interesting gotcha. Check it out:
app.root_url(domain: 'http://www.customdomain.org')
=> "http://www.http://www.customdomain.org/"
Thankfully, Rails gives us the host
option. Host will use a Regex to remove the protocol from the argument:
app.root_url(host: 'https://www.customdomain.org')
=> "http://www.customdomain.org/"
Notice the output in the last example; our protocol was replaced removed and replaced with http. You can use the protocol
argument to enforce your protocol on a given host
:
app.root_url(host: 'http://www.customdomain.org', protocol: 'https')
=> "https://www.customdomain.org/"
Tweet