对于Rails,如何访问或打印配置变量(作为实验或测试/调试)

时间:2021-11-01 00:06:07

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

例如,在Rails 3应用程序的config / environments / production.rb中,有

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 .yml files (as a hash and/or in some config object?) and in config/initializers, such as for

另外,如何打印.yml文件中的值(作为哈希和/或某些配置对象?)和config / initializers中的值,例如

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组件的所有信息?

3 个解决方案

#1


50  

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加载当前环境的值,而任何自定义的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类中手动找到这些设置。

#2


4  

You can also use AppName::Application.config (where AppName is the name of your application) to access the Rails::Application::Configuration object.

您还可以使用AppName :: Application.config(其中AppName是您的应用程序的名称)来访问Rails :: Application :: Configuration对象。

$ AppName::Application.config == Rails.application.config
true

#3


2  

Like idlefingers pointed out, for newer versions of rails, Rails.application.config.my_variable should 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 ENV constant, like so: ENV['my_variable']

但是,如果因为你坚持使用较旧版本的Rails(2.3等)而无效,那么可以使用ENV常量,如下所示:ENV ['my_variable']

#1


50  

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加载当前环境的值,而任何自定义的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类中手动找到这些设置。

#2


4  

You can also use AppName::Application.config (where AppName is the name of your application) to access the Rails::Application::Configuration object.

您还可以使用AppName :: Application.config(其中AppName是您的应用程序的名称)来访问Rails :: Application :: Configuration对象。

$ AppName::Application.config == Rails.application.config
true

#3


2  

Like idlefingers pointed out, for newer versions of rails, Rails.application.config.my_variable should 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 ENV constant, like so: ENV['my_variable']

但是,如果因为你坚持使用较旧版本的Rails(2.3等)而无效,那么可以使用ENV常量,如下所示:ENV ['my_variable']