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

前端 未结 4 1960
野的像风
野的像风 2020-12-25 11:22

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 cou

相关标签:
4条回答
  • 2020-12-25 11:30

    use following:-

     default:
           cv1: Foo
           cv2: <%= ENV['FOOVAR'] %>
    
    0 讨论(0)
  • 2020-12-25 11:34

    Use <%= ENV['FOOVAR'] %> instead of <% ENV['FOOVAR'] %>.

    Be aware that this approach will only work if whatever is parsing the Yaml file is set up to process it via Erb (for example, you can see how Mongoid does exactly this). It's not universally supported in Yaml files though, so it depends on what you're using this Yaml file for.

    0 讨论(0)
  • 2020-12-25 11:45

    I was able to resolve this with simple code

    data = {}
    YAML.load_file("path_of_file/settings.yml").each do |key, value|
      data[key] = ENV[value] || value
    end 
    

    where my data was something like this

    account: ACCOUNT_USERNAME
    password: ACCOUNT_PASSWORD
    port: 5002
    

    and ACCOUNT_USERNAME and ACCOUNT_PASSWORD are environment variables

    0 讨论(0)
  • 2020-12-25 11:48

    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.

    0 讨论(0)
提交回复
热议问题