Ruby-on-rails 对于 Rails,如何访问或打印配置变量(作为实验或测试/调试)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5288476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
For Rails, how to access or print out config variables (as experiment or test / debugging)
提问by nonopolarity
For example, in config/environments/production.rbin a Rails 3 app, there is
例如,在config/environments/production.rbRails 3 应用程序中,有
config.serve_static_assets = false
and many variables. How can they be all printed out as a whole (perhaps in an object, instead of specifying each one-by-one) (print out in a view, such as FooController#index), just for looking into what types of values are available and to see what they are set to?
和很多变数。如何将它们作为一个整体打印出来(可能在一个对象中,而不是一个一个地指定每个)(在视图中打印出来,例如FooController#index),只是为了查看哪些类型的值可用并查看它们的设置?
Also, how to print out the values in the .ymlfiles (as a hash and/or in some config object?) and in config/initializers, such as for
此外,如何打印出.yml文件中的值(作为散列和/或某些配置对象?)和 in config/initializers,例如 for
MyAppFoo::Application.config.session_store :active_record_store
I found that we can print out the content of
我发现我们可以打印出内容
ActiveRecord::Base.configurations
but not
但不是
ActionController::Base.configurations
are there ways to print out all info of the MVC components?
有没有办法打印出 MVC 组件的所有信息?
回答by idlefingers
Most of the Rails config stuff can be accessed through:
大多数 Rails 配置内容可以通过以下方式访问:
Rails.application.config.<your_variable>
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...
关于在 config 中打印 .yml 文件的值,您必须自己做,因为 Rails 只会从database.yml. 这是您可以将它们全部加载的一种方法......
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.
关于初始化程序中设置的设置,如果它是 Rails 配置选项(例如您作为示例提供的会话存储),那么它将通过Rails.application.config. 如果没有,(例如 gem 的配置),那么您将不得不从 gem 类中手动查找这些设置。
回答by Dennis
You can also use AppName::Application.config(where AppNameis the name of your application) to access the Rails::Application::Configurationobject.
您还可以使用AppName::Application.config(其中AppName是您的应用程序的名称)来访问该Rails::Application::Configuration对象。
$ AppName::Application.config == Rails.application.config
true
回答by broox
Like idlefingers pointed out, for newer versions of rails, Rails.application.config.my_variableshould get you what you're looking for.
就像 idlefingers 指出的那样,对于较新版本的 rails,Rails.application.config.my_variable应该可以满足您的需求。
However, if that doesn't work because you're stuck with an older version of Rails (2.3, etc) you can use the ENVconstant, like so: ENV['my_variable']
但是,如果这不起作用,因为您坚持使用旧版本的 Rails(2.3 等),则可以使用ENV常量,如下所示:ENV['my_variable']

