rails 3,如何在设置中使用ENV配置vars。yml文件?

时间:2022-10-18 14:30:33

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

在我的设置。yml文件我有几个配置vars,其中一些引用ENV[]变量。

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

例如,我有ENV['FOOVAR'] =小部件。

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

我想我可以这样引用<% >中的ENV vars:

Settings.yml:

Settings.yml:

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

in rails console if I type

如果我输入,在rails控制台。

> ENV['FOOVAR']
=> WIDGET

but

> Settings.cv1
=> Foo   (works okay)
> Settings.cv2
=>nil   (doesn't work???)

3 个解决方案

#1


60  

use following:-

使用以下:-

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

#2


24  

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

上面的解决方案对我不起作用。但是,我找到了如何在YAML文件中使用变量的解决方案。

My .yml file contained something like:

我的。yml文件包含如下内容:

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.

因此,当您在.yml文件中引入scriptlet标记时,它更多的是erb模板。因此,首先将它作为erb模板读取,然后按上面所示加载yml。

#3


23  

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

使用< % = ENV[' FOOVAR ']% >的< % 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.

请注意,这种方法只会在任何解析Yaml文件的情况下工作(例如,您可以看到Mongoid是如何做到这一点的)。但是在Yaml文件中并不普遍支持它,因此它取决于您使用这个Yaml文件的目的。

#1


60  

use following:-

使用以下:-

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

#2


24  

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

上面的解决方案对我不起作用。但是,我找到了如何在YAML文件中使用变量的解决方案。

My .yml file contained something like:

我的。yml文件包含如下内容:

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.

因此,当您在.yml文件中引入scriptlet标记时,它更多的是erb模板。因此,首先将它作为erb模板读取,然后按上面所示加载yml。

#3


23  

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

使用< % = ENV[' FOOVAR ']% >的< % 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.

请注意,这种方法只会在任何解析Yaml文件的情况下工作(例如,您可以看到Mongoid是如何做到这一点的)。但是在Yaml文件中并不普遍支持它,因此它取决于您使用这个Yaml文件的目的。