Rails - 在yml文件中使用ENV变量

时间:2022-01-29 23:34:42

This is myfile.yml

这是myfile.yml

this_is_key:
  - [<%= ENV['MY_KEY_NAME'] %>, 1]

It will return the error syntax, can not parse file.Now, i change like this:

它将返回错误语法,无法解析file.Now,我改变如下:

this_is_key:
  - [my_key_here, 1]

No errors occurred. Somebody can tell me, how can i pass ENV variable in my file?

没有发生错误。有人可以告诉我,我如何在我的文件中传递ENV变量?

1 个解决方案

#1


1  

You can do it only if you parse the resulting yml file as an erb template:

只有在将生成的yml文件解析为erb模板时,才能执行此操作:

YAML.load(ERB.new(File.read("myfile.yml")).result)

result method passes the current binding into template and renders the file according to the variables in the scope.

result方法将当前绑定传递给模板,并根据范围中的变量呈现文件。

Here's an article about such experiments.

这是一篇关于此类实验的文章。

#1


1  

You can do it only if you parse the resulting yml file as an erb template:

只有在将生成的yml文件解析为erb模板时,才能执行此操作:

YAML.load(ERB.new(File.read("myfile.yml")).result)

result method passes the current binding into template and renders the file according to the variables in the scope.

result方法将当前绑定传递给模板,并根据范围中的变量呈现文件。

Here's an article about such experiments.

这是一篇关于此类实验的文章。