为我的 Rails 应用程序创建自定义配置选项的最佳方式?
我需要为我的 Rails 应用程序创建一个配置选项。 对于所有环境来说它都可以是相同的。 我发现如果我在 environment.rb
中设置它,它就可以在我的视图中使用,这正是我想要的......
environment.rb
AUDIOCAST_URI_FORMAT = http://blablalba/blabbitybla/yadda
效果很好。
不过,我有点不安。 这是一个好方法吗? 有没有更时髦的方法?
I need to create one config option for my Rails application. It can be the same for all environments. I found that if I set it in environment.rb
, it's available in my views, which is exactly what I want...
environment.rb
AUDIOCAST_URI_FORMAT = http://blablalba/blabbitybla/yadda
Works great.
However, I'm a little uneasy. Is this a good way to do it? Is there a way that's more hip?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
对于不需要存储在数据库表中的一般应用程序配置,我喜欢在 config 目录中创建一个
config.yml
文件。 对于您的示例,它可能如下所示:此配置文件从 config/initializers 中的自定义初始值设定项加载:
如果您使用的是 Rails 3,请确保不会意外添加前导斜杠你的相对配置路径。
然后,您可以使用以下方法检索该值:
请参阅此 Railscast 了解完整详细信息。
For general application configuration that doesn't need to be stored in a database table, I like to create a
config.yml
file within the config directory. For your example, it might look like this:This configuration file gets loaded from a custom initializer in config/initializers:
If you're using Rails 3, ensure you don't accidentally add a leading slash to your relative config path.
You can then retrieve the value using:
See this Railscast for full details.
Rails 3 版本的初始化代码如下(RAILS_ROOT 和 RAILS_ENV 已弃用)
APP_CONFIG = YAML.load_file(Rails.root.join('config', 'config.yml'))[Rails.env]
另外,Ruby 1.9.3 使用 Psych,它使合并键区分大小写,因此您需要更改配置文件以考虑到这一点,例如
Rails 3 version of initialiser code is as follows (RAILS_ROOT & RAILS_ENV are deprecated)
APP_CONFIG = YAML.load_file(Rails.root.join('config', 'config.yml'))[Rails.env]
Also, Ruby 1.9.3 uses Psych which makes merge keys case sensitive so you'll need to change your config file to take that into account, e.g.
Rails >= 4.2
只需在
config/
目录中创建一个YAML
文件,例如:config/neo4j.yml
。neo4j.yml 的内容可以如下所示(为简单起见,我在所有环境中使用默认值):
在 config/application.rb 中:
现在,您的自定义配置可以访问如下:
更多信息
Rails官方API文档将
config_for
方法描述为:如果您不想使用
yaml
文件正如 Rails 官方指南所述:
示例
官方
config_for
方法参考 |官方 Rails 指南
Rails >= 4.2
Just create a
YAML
file intoconfig/
directory, for example:config/neo4j.yml
.Content of
neo4j.yml
can be somthing like below(For simplicity, I used default for all environments):in
config/application.rb
:Now, your custom config is accessible like below:
More info
Rails official API document describes
config_for
method as:If you do not want to use a
yaml
fileAs Rails official guide says:
Example
Official Reference for
config_for
method |Official Rails Guide
第 1 步: 创建 config/initializers/appconfig.rb
第 2 步: 创建 config/config.yml
第 3 步: 在代码中的任意位置获取常量
Step 1: Create config/initializers/appconfig.rb
Step 2: Create config/config.yml
Step 3: Get constants anywhere in the code
我只是想更新它以获取 Rails 4.2 和 5 中最新的酷东西,您现在可以在任何
config/**/*.rb
文件中执行此操作:(这是一个文字
x
在那里,即config.x.
字面上必须是这样,然后您可以在x
之后添加任何您想要的内容)...这将在您的应用程序中提供:
在此处查看更多信息:http://guides。 rubyonrails.org/configuring.html#custom-configuration
I just wanted to update this for the latest cool stuff in Rails 4.2 and 5, you can now do this inside any of your
config/**/*.rb
files:(and that's a literal
x
in there, ie. theconfig.x.
literally must be that, and then you can add whatever you want after thex
)...and this will be available in your app as:
See more here: http://guides.rubyonrails.org/configuring.html#custom-configuration
关于此主题的一些额外信息:
“.with_in Different_access”允许您使用字符串键或等效的符号键访问哈希中的值。
例如。
APP_CONFIG['audiocast_uri_format'] =>; 'http://blablalba/blabbitybla/yadda'
APP_CONFIG[:audiocast_uri_format] => APP_CONFIG[:audiocast_uri_format] => 'http://blablalba/blabbitybla/yadda'
纯粹是为了方便,但我更喜欢将我的键表示为符号。
Just some extra info on this topic:
".with_indifferent_access" allows you to access the values in the hash using a string key or with an equivalent symbol key.
eg.
APP_CONFIG['audiocast_uri_format'] => 'http://blablalba/blabbitybla/yadda'
APP_CONFIG[:audiocast_uri_format] => 'http://blablalba/blabbitybla/yadda'
Purely a convenience thing, but I prefer to have my keys represented as symbols.
我使用类似于 John for Rails 3.0/3.1 的东西,但我首先让 erb 解析文件:
这允许我在需要时在我的配置中使用 ERB,例如读取 heroku 的 redistogo url:
I use something similar to John for Rails 3.0/3.1, but I have erb parse the file first:
This allows me to use ERB in my config if I need to, like reading heroku's redistogo url:
Rails 4
创建自定义配置 yaml 并加载它(并使其可供您的应用程序使用),与
database_configuration
类似。创建您的
*.yml
,在我的例子中,我需要一个 redis 配置文件。config/redis.yml
然后加载配置
config/application.rb
访问值:
Rails.configuration.redis_configuration[Rails.env]
类似如何通过Rails.configuration.database_configuration[Rails.env]
访问database.yml
Rails 4
To create a custom configuration yaml and load it (and make available to your app) similar to how
database_configuration
.Create your
*.yml
, in my case I needed a redis configuration file.config/redis.yml
Then load the configuration
config/application.rb
Access the values:
Rails.configuration.redis_configuration[Rails.env]
similar to how you can have access to yourdatabase.yml
byRails.configuration.database_configuration[Rails.env]
基于 Omer Aslam 的优雅解决方案,我决定将按键转换为符号。 唯一的变化是:
这允许您通过符号作为键来引用值,例如,
这在我看来更整洁。
(作为答案发布,因为我的声誉不够高,无法评论奥马尔的回复)
Building on Omer Aslam's elegant solution, I decided to convert the keys into symbols. The only change is:
This allows you to then reference values by symbols as keys, e.g.
This seems neater to my eyes.
(Posted as an answer as my reputation isn't high enough to comment on Omer's reply)
我喜欢 simpleconfig。 它允许您进行每个环境的配置。
I like simpleconfig. It allows you to have per environment configuration.
请参阅我对 的回复存储应用程序参数的最佳位置在哪里:数据库、文件、代码...? 与
您所拥有的内容的一个变体,它是对另一个文件的简单引用。 它发现environment.rb不会不断更新,并且其中没有一堆特定于应用程序的内容。
虽然没有具体回答您的“这是 Rails 方式吗?”的问题,但也许会对此进行一些讨论。
see my response to Where is the best place to store application parameters : database, file, code...?
A variation to what you had in that it's a simple reference to another file. It sees that environment.rb isn't constantly updated and doesn't have a heap of app specific stuff in it.
Though not a specific answer to your question of 'is it the Rails way?', perhaps there'll be some discussion there about that.
我更喜欢通过全局应用程序堆栈访问设置。 我避免在局部范围内出现过多的全局变量。
config/initializers/myconfig.rb
并访问它。
I prefer accessing settings through the global application stack. I avoid excess global variables in local scope.
config/initializers/myconfig.rb
And access it with.
我在 Rails 初始化之前加载设置的方法
允许您在 Rails 初始化中使用设置并配置每个环境的设置
您可以通过两种方式获取设置:
设置['电子邮件']或Settings.email
My way to load Settings before Rails initialize
Allows you to use settings in Rails initialization and configure settings per environment
You could get settings in two ways:
Settings['email'] or Settings.email
我自定义配置的最佳方法是在设置.yml 丢失时发出消息。
从 config/initializers/custom_config.rb 中的自定义初始值设定项加载
在 config/setting.yml 中创建 YAML
My best way to custom config, with raise message when setting.yml is missing.
gets loaded from a custom initializer in config/initializers/custom_config.rb
Create a YAML in config/setting.yml