For Rails, how to access or print out config variables (as experiment or test / debugging)

后端 未结 3 1151
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-02 06:18

For example, in config/environments/production.rb in a Rails 3 app, there is

config.serve_static_assets = false

and many variables

3条回答
  •  野性不改
    2021-02-02 06:47

    Most of the Rails config stuff can be accessed through:

    Rails.application.config.
    

    With regards to printing out the values of .yml files in config, you'd have to do that yourself becuase Rails will only load up the values for the current environment from database.yml, and any custom yml config files will be just that - custom. Here's one way you could load them all up...

    all_configs = []
    Dir[Rails.root.join("config/*.yml")].each {|f| all_configs << YAML.load_file(f) }
    

    With regards to settings set in initializers, if it's a Rails config option (such as the session store which you've given as an example), then it will be available through Rails.application.config. If not, (for example configuration for a gem) then you will have to manually find those settings from the gem classes.

提交回复
热议问题