0

I'm pretty new in rails and I would like to ask different database regarding to the origin of the request.

Something like :

http://localhost:80     env: production    database: production
http://localhost:8080   env: development   database: development
http://localhost:8081   env: test          database: test

I already have my three environments defined. I just want to switch !

According to this post, in my application.rb I could adapt

Rails.env = ActiveSupport::StringInquirer.new('production')

to

# application.rb
if request.headers['HTTP_ORIGIN'] == 'http://localhost:80'
    Rails.env = ActiveSupport::StringInquirer.new('production')
elsif request.headers['HTTP_ORIGIN'] == 'http://localhost:8080'
    Rails.env = ActiveSupport::StringInquirer.new('development')
elsif request.headers['HTTP_ORIGIN'] == 'http://localhost:8081'
    Rails.env = ActiveSupport::StringInquirer.new('test')

but it seems I haven't access to method headers :

C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/railtie/configurable.rb:30:in `method_missing': undefined method `request' for #<App::Application:0x3e9c088> (NoMethodError)
    from C:/Sites/KML-backend/config/application.rb:88:in `<class:Application>'
    from C:/Sites/KML-backend/config/application.rb:26:in `<module:App>'
    from C:/Sites/KML-backend/config/application.rb:25:in `<top (required)>'
    from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:53:in `require'
    from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:53:in `block in <top (required)>'
    from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:50:in `tap'
    from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:50:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
Community
  • 1
  • 1
Sylvain
  • 1,399
  • 2
  • 17
  • 25

1 Answers1

0

I don't really see why you would want to do this, but how about starting three servers instead?

RAILS_ENV="production" rails s -p 80
RAILS_ENV="development" rails s -p 8080
RAILS_ENV="test" rails s -p 8081
svoop
  • 3,318
  • 1
  • 23
  • 41
  • Thanks for your response svoop. I would prefer have just one running server which dispatch requests instead of three instances. That's why I wanted to programatically switch environment. – Sylvain Feb 02 '14 at 10:32
  • I'm not sure this can be done at all since the different environments usually use very different settings e.g. for caching models or handling the asset pipeline. – svoop Feb 03 '14 at 18:59