如何在 Rails 中启动时加载配置?

发布于 2024-09-28 21:43:44 字数 89 浏览 0 评论 0原文

我的 YAML 文件中有一些配置值,需要在应用程序启动时加载。这些值需要在几个不同的地方访问(在一些模型和一些控制器中)。加载、存储和访问这些内容的最佳方式是什么?

I have some configuration values in a YAML file that needs loaded when my app starts up. The values need to be accessed in a few different places (both in a few models and a few controllers). What is the best way to load, store, and access these?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北方的韩爷 2024-10-05 21:43:44

您可以执行以下操作:

创建文件 yml 示例 test.yml :

key: 936QQ84d3c4m8Y4Y

在 config/initializers 中创建文件。

test = YAML.load_file("#{RAILS_ROOT}/config/test.yml")
KEY = test["key"]

You can do as follows

create file yml example test.yml :

key: 936QQ84d3c4m8Y4Y

create file in config/initializers.

test = YAML.load_file("#{RAILS_ROOT}/config/test.yml")
KEY = test["key"]
墟烟 2024-10-05 21:43:44

一个初始化器。 John Nunemaker 发布了他用于 Harmony 的over on gist

因此,在该示例中,harmony.rb 将位于 config/initializers 中,而 Harmony.yml 将仅位于 config 中。

An initializer. John Nunemaker posted the one he uses for Harmony over on gist.

So in that example harmony.rb would go in config/initializers and harmony.yml would just be in config.

落墨 2024-10-05 21:43:44

请检查这个 Gem“mc-settings”,它从 YML 文件管理应用程序设置并允许基于 Rails 环境等进行覆盖。

https://github.com/modcloth/mc-settings

Please check this Gem "mc-settings" that manages application settings from YML files and allows overrides based on Rails environment, etc.

https://github.com/modcloth/mc-settings

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文