2

I want to use in my code (in views as well) variables like:

ENV['SERVER_URL1'] 

And want them to be different for diffident environments (prod, dev, test)

  1. Were and how should I set them up?
  2. Is this (using ENV vars) a right way to configure application for different environments?
  3. about ENV['SERVER_URL'] - is it a standard variable? When does it becomes available.

I tried to set in different parts of application (application.rb, development.rb)

ENV['SERVER_URL1'] = 'http://localhost:4000/'

but it seems not to work.

Mischa
  • 42,876
  • 8
  • 99
  • 111
WHITECOLOR
  • 24,996
  • 37
  • 121
  • 181
  • See [this question](http://stackoverflow.com/questions/4800028/rails-3-setting-custom-environment-variables). Do you really need them to be environment variables? – Dave Newton Nov 30 '11 at 14:11
  • Well I want this variables to be different for local dev and prod heroku deploy, on heroku I can add ENV variables using config:add, but I don't know how to accomplish this task in my local dev environment. – WHITECOLOR Nov 30 '11 at 14:19
  • 3
    Right, so you'd check for the environment in the initializer, and set appropriately. – Dave Newton Nov 30 '11 at 14:20
  • I inserted to application.erb: ENV['SERVER_URL1'] = 'http://localhost:4000/path' but this value is not available in app. The same I did in development.rb – WHITECOLOR Nov 30 '11 at 14:31
  • 1
    Did you read the link I provided? They don't do it like that. That's why I asked if you needed them to *specifically* be environment variables. – Dave Newton Nov 30 '11 at 14:33
  • Yes, I've read, thanks for the link. But is not it possible to accomplish using ENV variables without custom Configuration class? Or I just don't understand what ENV variables for? – WHITECOLOR Nov 30 '11 at 14:41
  • They're for whatever you want, I just don't yet know why they'd be preferred over initializer-based code in this usecase, which is why I've asked. – Dave Newton Nov 30 '11 at 14:44
  • Ok it seems I got it. BTW one more newbie question: how to make this Configuration.propery avaliable for using in erb views? – WHITECOLOR Nov 30 '11 at 14:51
  • Should just be available, although see the link regarding Rails 3.1. See the other answer in that link regarding the use of globals as an alternative, although I guess I'd avoid that, but that might just be a personal preference. – Dave Newton Nov 30 '11 at 14:53
  • When I try to use it in in my view: <%= Configuration.server_url %> there the error occurs: uninitialized constant ActionView::CompiledTemplates::Configuration – WHITECOLOR Nov 30 '11 at 15:00
  • Aaaand what version of Rails are you using? Could also expose it as a controller variable if it's not automagically available. – Dave Newton Nov 30 '11 at 15:02
  • ruby 1.9.3 rails 3.1.3, ok I'll try thanks a lot for your help! – WHITECOLOR Nov 30 '11 at 15:05

1 Answers1

1

When using Rails 4.1+, the new and preferred way to set ENV variables is to use the config/secrets.yml file.

Here is an excerpt from the 4.1 release notes

The secrets added to this file are accessible via Rails.application.secrets. For example, with the following config/secrets.yml:

development:
  secret_key_base: 3b7cd727ee24e8444053437c36cc66c3
  some_api_key: SOMEKEY

Rails.application.secrets.some_api_key returns SOMEKEY in the development environment.

See the Upgrading Ruby on Rails guide on how to migrate existing applications to use this feature.

So you should set:

development:
  SERVER_URL1: http://localhost:4000
production:
  SERVER_URL1: http://my-domain.com
sealocal
  • 10,897
  • 3
  • 37
  • 50