问题描述
我正在使用Rails 4,并且我想使用自定义配置功能,如下所述:
I'm using Rails 4, and I would like to use the custom configuration functionality as explained here:
我创建了以下YAML文件( config\prefs.yml
):
I created the following YAML file (config\prefs.yml
):
development:
password: test
然后我将此添加到了 config / application.rb
:
module MyApp
class Application < Rails::Application
# ...
config.x.prefs = Rails.application.config_for(:prefs)
end
end
当我进入Rails控制台时,得到以下信息:
When I go to the rails console, I get this:
> Rails.configuration.x.prefs
=> {}
Rails为什么不能正确加载配置?
Why isn't Rails correctly loading the configuration?
推荐答案
我正在猜测以下内容:
- 您有 gem。
- 您的自定义配置以某种方式被初始化为当前状态。 (即为空)
- Spring不会跟踪
config\prefs.yml
,因此它不知道环境需要重新加载。
- You have the Spring gem bundled in.
- Your custom configuration somehow got initialized in the state it is currently in. (i.e. empty)
- The
config\prefs.yml
isn't tracked by Spring, so it doesn't know the environment needs to be reloaded.
如果我是对的,则只需使用以下代码创建一个初始化程序即可: p>
If i'm correct, you'll just have to create an initializer with the following code:
Spring.watch "config/prefs.yml"
当然,每次更改配置时,您都必须重新加载控制台。为此,我设法重现并解决了您的问题,希望对您有所帮助。
And, of course, you'll have to reload the console each time the config is changed. I've managed to reproduce and solve your issue with this, so i hope this helps.
这篇关于Rails定制配置返回空哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!