简体   繁体   English

Rails `config_for` 返回一个空的哈希值

[英]Rails `config_for` returning an empty hash

I saw this question yet the solution did not work for me.我看到了这个问题,但该解决方案对我不起作用。 As per the rails docs , I've taken the following steps:根据rails docs ,我采取了以下步骤:

I've created a file config/benchmarks.yml , with the following content:我创建了一个文件config/benchmarks.yml ,内容如下:

Test: "This"

I also added config.benchmarks = config_for(:benchmarks) in the config/application.rb file.我还在config/application.rb文件中添加了config.benchmarks = config_for(:benchmarks)

I also added the path to config/spring.rb yet when I go to the console and enter Rails.application.config_for(:benchmarks) it returns an empty hash.我还添加了config/spring.rb Rails.application.config_for(:benchmarks)的路径,但当我转到控制台并输入Rails.application.config_for(:benchmarks)它返回一个空哈希。

What am I missing here?我在这里缺少什么? I'm running Rails 5.2.1.我正在运行 Rails 5.2.1。

No need to touch config/spring.rb file.无需触摸config/spring.rb文件。 Just make sure you are passing as like in sample below.只要确保你像下面的示例一样通过。

# config/benchmarks.yml
development:
  test: test

#application.rb
config.benchmarks = config_for(:benchmarks)

#rails console
Rails.application.config_for(:benchmarks)

=> {
     "test" => "test"
   } 

config_for wants to have a "section" to pull. config_for 想要拉出一个“部分”。 That section is the value in Rails.env.该部分是 Rails.env 中的值。 If you just have a "flat" .yml which can be valid YML you'll get an empty set.如果您只有一个可以是有效 YML 的“平面” .yml,您将得到一个空集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM