3

I have bunch of routes( ~50)which needs to be mapped to external URL. I can do definitely as suggested in here but that will clutter my routes.rb file. Is there any way I can have them in an config file and refer to it from my routes.rb file?

Also, when mapping to external URL, if it non production environment, it needs to be mapped to "http:example-test.com/.." and in production mode it needs to be mapped to "http:example.com/...". I know I can have a yml file in config which deals with differnt environments. But how do I access it in my routes.rb file?

Community
  • 1
  • 1
user2452057
  • 816
  • 2
  • 11
  • 23

2 Answers2

8

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"}
max
  • 96,212
  • 14
  • 104
  • 165
3

Try this. Hope this will work for you ..

MyApp::Application.routes.draw do
  # External urls
  scope host: 'www.example.com' do
    get 'thing' => 'dev#null', as: :thing
  end
end

# Use thing_url in your veiws (thing_path would not include the host)
# thing_url => "http://www.example.com/thing"
sushilprj
  • 2,203
  • 1
  • 14
  • 19
  • You could cope with the production/non-production requirement here by putting in a tertiary statement like `scope host: (Rails.env.production? ? 'www.example.com' : 'example-test.com') do` – Max Williams Oct 12 '15 at 13:51
  • Nice idea @MaxWilliams but I would use a config var: `scope host: Rails.configuration.external_host`. You can then set up the different values in `/config/development.rb`, `/config/production.rb` ... – max Oct 12 '15 at 14:42
  • It would be a custom configuration param. See my answer. – max Oct 12 '15 at 15:01
  • Thanks @max. I just saw that pop up and deleted my comment. That answers my question perfectly. – Elvn Oct 12 '15 at 15:01
  • Yep, that is cleaner than an inline tertiary statement. – Max Williams Oct 12 '15 at 15:10