First let's create a custom configuration variable for the external host:
# config/application.rb
module MyApp
class Application < Rails::Application
config.external_host = ENV["EXTERNAL_HOST"]
end
end
And then lets set it up per environment:
# config/environments/development.rb
Rails.application.configure do
# ...
config.external_host ||= 'dev.example.com'
end
# config/environments/test.rb
Rails.application.configure do
# ...
config.external_host ||= 'test.example.com'
end
# config/environments/production.rb
Rails.application.configure do
# ...
config.external_host ||= 'example.com'
end
Then we setup the route:
Rails.application.routes.draw do
# External urls
scope host: Rails.configuration.external_host do
get 'thing' => 'dev#null', as: :thing
end
end
And lets try it out:
$ rake routes
Prefix Verb URI Pattern Controller#Action
thing GET /thing(.:format) dev#null {:host=>"dev.example.com"}
$ rake routes RAILS_ENV=test
Prefix Verb URI Pattern Controller#Action
thing GET /thing(.:format) dev#null {:host=>"test.example.com"}
$ rake routes RAILS_ENV=production
Prefix Verb URI Pattern Controller#Action
thing GET /thing(.:format) dev#null {:host=>"example.com"}
$ rake routes EXTERNAL_HOST=foobar
Prefix Verb URI Pattern Controller#Action
thing GET /thing(.:format) dev#null {:host=>"foobar"}