Rails:每个环境的初始化程序? [英] Rails: Per-environment initializers?
问题描述
我希望我的一个初始化程序(在 config/initializers/
中)中的代码仅在 :development
环境中运行,而不是 :test
或 :production
.最好的方法是什么?
I'd like the code in one of my initializers (in config/initializers/
) to be run only for the :development
environment, but not :test
or :production
. What's the best way to do that?
将其粘贴到 config/environments/test.rb
似乎不干净,而且我不太喜欢将整个初始化文件包装在 if Rails.env == 'development' do... 结束
语句.是否有一些规范的方法可以做到这一点?
Pasting it into config/environments/test.rb
seems unclean, and I don't quite like wrapping the entire initializer file in an if Rails.env == 'development' do ... end
statement. Is there some canonical way to do this?
(背景:为了加快测试加载时间,我试图将 Barista gem 移动到我的 Gemfile
的 :development
组中,但是 config/initializers/barista_config.rb
调用 Barista.configure
,所以现在它在测试(和生产)模式下窒息.)
(Background: To speed up test load times, I'm trying to move the Barista gem into the :development
group of my Gemfile
, but config/initializers/barista_config.rb
calls Barista.configure
, so now it chokes on that in test (and production) mode.)
推荐答案
我很确定你只有两个选择是将配置代码放在 config/environments/development.rb
或包装你的初始化程序使用 if
块编写代码.不过,您可以通过执行 if Rails.env.development?
来收紧第二个选项.
I'm pretty sure your only two options are putting the configuration code in config/environments/development.rb
or wrapping your initializer code with your if
block. You can tighten up your second option by doing if Rails.env.development?
, though.
这篇关于Rails:每个环境的初始化程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!