我试图在yaml中存储一些配置变量,表示为关联数组,也就是字典。我是这样做的:
content_prices:
- {country: AU, price: 6990000}
- {country: AT, price: 4990000}
- {country: BE, price: 4990000}
当我尝试从我的ROR init文件解析它时会产生异常:
undefined方法`symbolize_keys!'为零:NilClass
以下是我如何创建它:
Config = YAML.load_file("#{Rails.root}/config/prices.yml")[Rails.env].symbolize_keys!
我猜我的yaml语法错了,那怎么写得好呢?
你的YAML看起来没问题,或者你可以像这样配置一个哈希数组:
content_prices:
- country: AU
price: 6990000
- country: AT
price: 4990000
- country: BE
price: 4990000
哪个将加载为以下哈希:
{"content_prices"=>[
{"country"=>"AU", "price"=>6990000},
{"country"=>"AT", "price"=>4990000},
{"country"=>"BE", "price"=>4990000}]}
但是仍然没有在主哈希中给你任何关于Rails.env
的引用。问题似乎是你期望在你的哈希中而不是YAML的格式。
不是在轨道上,但在Symfony2 php上,我不得不像这样配置yml文件:
content_prices: - country: AU price: 6990000 - country: AT price: 4990000 - country: BE price: 4990000
如果有人想要使用动态密钥,也可以:
AppBundle\Service\MailerService:
lazy: false
arguments:
$defaultFrom:
'%mailer_user%': '%mailer_name%'