What is the best way to store app specific configuration in rails?

后端 未结 6 578
心在旅途
心在旅途 2020-12-30 14:56

I need to store app specific configuration in rails. But it has to be:

  • reachable in any file (model, view, helpers and controllers
  • environment specifi
6条回答
  •  一整个雨季
    2020-12-30 15:09

    I was helping a friend set up the solution mentioned by Ricardo yesterday. We hacked it a bit by loading the YAML file with something similar to this (going from memory here):

    require 'ostruct'
    require 'yaml'
    require 'erb'
    #config = OpenStruct.new(YAML.load_file("#{RAILS_ROOT}/config/config.yml"))
    config = OpenStruct.new(YAML.load(ERB.new(File.read("#{RAILS_ROOT}/config/config.yml")).result))
    env_config = config.send(RAILS_ENV)
    config.common.update(env_config) unless env_config.nil?
    ::AppConfig = OpenStruct.new(config.common)
    

    This allowed him to embed Ruby code in the config, like in Rhtml:

    development:
      path_to_something: <%= RAILS_ROOT %>/config/something.yml
    

提交回复
热议问题