Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails 3, how use an ENV config vars in a Settings.yml file?

In my settings.yml file I have several config vars, some of which reference ENV[] variables.

for example I have ENV['FOOVAR'] equals WIDGET

I thought I could reference ENV vars inside <% %> like this:

Settings.yml:

default:    cv1: Foo    cv2: <% ENV['FOOVAR'] %> 

in rails console if I type

> ENV['FOOVAR'] => WIDGET 

but

> Settings.cv1 => Foo   (works okay) > Settings.cv2 =>nil   (doesn't work???) 
like image 478
jpw Avatar asked May 03 '11 06:05

jpw


People also ask

Can I use variables in .ENV file?

You can set default values for environment variables using a .env file, which Compose automatically looks for in project directory (parent folder of your Compose file).

How do you use an environment variable in Ruby?

To pass environment variables to Ruby, simply set that environment variable in the shell. This varies slightly between operating systems, but the concepts remain the same. To set an environment variable on the Windows command prompt, use the set command.


2 Answers

use following:-

 default:        cv1: Foo        cv2: <%= ENV['FOOVAR'] %> 
like image 190
Anubhaw Avatar answered Sep 23 '22 18:09

Anubhaw


The above solution did not work for me. However, I found the solution on How do I use variables in a YAML file?

My .yml file contained something like:

development: gmail_username: <%= ENV["GMAIL_USERNAME"] %> gmail_password: <%= ENV["GMAIL_PASSWORD"] %> 

The solution looks like:

template = ERB.new File.new("path/to/config.yml.erb").read processed = YAML.load template.result(binding) 

So when you introduce a scriptlet tag in .yml file, it is more of erb template. So read it as a erb template first and then load the yml as shown above.

like image 43
arpiagar Avatar answered Sep 25 '22 18:09

arpiagar