Rails 3 :: config / initializers分隔每个环境的目录

时间:2011-11-03 21:13:32

标签: ruby-on-rails ruby ruby-on-rails-3

在Rails 3 +中每个环境的单独子目录中分离初始值设定项的最佳方法是什么?

我听说配置/初始化程序的所有子目录都将被选中,因此每个环境的子目录都无法工作,除非创造性地修补猴子。

示例:config /.../ initializers /< environment> / * .rb

2 个答案:

答案 0 :(得分:2)

# config/application.rb

module MyProject
  class Application < Rails::Application

  # modifies initializers to load as follows:
  #
  # 1. config/initializers/*.rb
  # 2. config/initializers/environments/#{Rails.env}/**/*.rb
  #
  unless config.paths['config/initializers_environment']
    old_initializers_dir = config.paths['config/initializers']
    config.paths['config/initializers'] = Dir[File.join(old_initializers_dir, '*.rb')]
    config.paths['config/initializers_environment'] = Dir[File.join(old_initializers_dir, 'environments', Rails.env, '**', '*.rb')]
    initializer :load_per_environment_initializers do
      config.paths['config/initializers_environment'].to_a.sort.each do |initializer|
        load(initializer)
      end
    end
  end

  # ...

https://gist.github.com/1338248

答案 1 :(得分:2)

这个答案避免了让Rails :: Engine正常工作的复杂性,并且归功于SF Ruby on Rails聚会和朋友的Bosco。它还允许通过命令行/ shell设置覆盖并与Heroku一起使用。

0)更改配置/初始化程序/ ** / *。rb看起来:

FB_SECRET_KEY = 'kjkjdsfkjalsfyoursecretherelkjsdfljkasdfljdjlf'

要:

FB_SECRET_KEY = ENV['APPNAME_FB_SECRET_KEY']

1)我在.gitignore中创建了一个config / secrets.rb,如下所示:

ENV['APPNAME_FB_SECRET_KEY'] ||= 'kjkjdsfkjalsfyoursecretherelkjsdfljkasdfljdjlf'

. . . 

2)添加此行以开始脚本/ rails:

require File.expand_path('../../config/secrets', __FILE__)

3)加入.gitignore

config/secrets.rb

Bonus)添加一个不在.gitignore中的config / secrets.rb.example模板文件。