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.

12
  • See this question. Do you really need them to be environment variables? Commented Nov 30, 2011 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. Commented Nov 30, 2011 at 14:19
  • 3
    Right, so you'd check for the environment in the initializer, and set appropriately. Commented Nov 30, 2011 at 14:20
  • I inserted to application.erb: ENV['SERVER_URL1'] = 'localhost:4000/path' but this value is not available in app. The same I did in development.rb Commented Nov 30, 2011 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. Commented Nov 30, 2011 at 14:33

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.