I was wondering if there is the best practice on where to store global settings in a rails app. What I mean by that is i.e: I have a few globals defined that may change, but not likely and it seems inappropriate to store them in DB since they are used so much. For instance I have SYSTEM_EMAIL & SYSTEM_EMAIL_SIGNATURE & SYSTEM_STORAGE_ROOT.
我想知道在一个rails应用程序中存储全局设置的最佳实践是什么,我的意思是。e:我定义了一些全局变量,它们可能会改变,但不太可能,而且由于它们被大量使用,所以在DB中存储它们似乎不合适。例如,我有SYSTEM_EMAIL & SYSTEM_EMAIL_SIGNATURE & SYSTEM_STORAGE_ROOT。
Right now I keep them in environment.rb, but I'm not sure if this is the right palce to store them.
现在我把它们放在环境中。rb,但我不确定这是否适合储存它们。
Thank you
谢谢你!
EDIT:
编辑:
Accepted answer still stands as appropriate, however I since moved on to using https://github.com/markbates/configatron , there are other options but I like configatron the most.
接受的答案仍然是适当的,但是我后来改用https://github.com/markbates/configatron,还有其他选项,但是我最喜欢configatron。
6 个解决方案
#1
15
One of my favourite techniques is to put a file containing the constants in directory config/initializers/
(all files in this directory are loaded automatically) but with a section for each different Rails environment. e.g.
我最喜欢的技术之一是在目录配置/初始化器/(这个目录中的所有文件都是自动加载的)中放置一个包含常量的文件,但是每个不同的Rails环境都有一个部分。如。
case ENV['RAILS_ENV']
when "development"
SYSTEM_EMAIL = ...
SYSTEM_EMAIL_SIGNATURE = ...
when "staging"
SYSTEM_EMAIL = ...
SYSTEM_EMAIL_SIGNATURE = ...
when "production"
SYSTEM_EMAIL = ...
SYSTEM_EMAIL_SIGNATURE = ...
end
If you want, instead, to load up all the constants in one big hash then you can load them as a YAML file. Create two files, one called, say, config/initializers/email_constants.rb
and the other config/email_constants.yml
. In the latter put something like:
如果您想要将所有常量加载到一个大的散列中,那么您可以将它们加载为YAML文件。创建两个文件,一个叫做配置/初始化器/email_constant。rb和其他配置/email_constants.yml。后一种说法是:
development:
:system_email: ...
:system_email_signature: ...
staging:
:system_email: ...
system_email_signature: ...
... etc ...
Then in config/initializers/email_constants.rb
put:
然后在config /初始化/ email_constants。rb说:
EMAIL_CONSTANTS = YAML.load_file("#{RAILS_ROOT}/config/email_constants.yml")[RAILS_ENV]
This loads the entire YAML file and the assigns the value of the appropriate key (which represents the RAILS_ENV
) to EMAIL_CONSTANTS
.
这将加载整个YAML文件,并将适当的键(代表RAILS_ENV)的值赋给EMAIL_CONSTANTS。
The advantage of both of these techniques is locality. You can place all the constants that are related to each other (i.e. email constants in this case) in one file. Also, instead of having the same constants spread across three different files (one for each Rails environment) you have them all in one file.
这两种技术的优点都是局部性。您可以将所有相关的常量放在一个文件中(在本例中为电子邮件常量)。而且,与其让相同的常量分布在三个不同的文件(每个Rails环境一个)上,不如将它们全都放在一个文件中。
#2
10
Because such values often change depending on the environment you're running in, I store globals in config/environments/development.rb|production.rb|test.rb
, with appropriate values for each environment.
因为这些值经常根据您所运行的环境而变化,所以我将全局变量存储在config/environments/development.rb|product .rb|测试中。rb,对每个环境都有适当的值。
#3
2
Rails 3 introduces the Application object. Even with Rails 2, you might want to store your globals in a similar manner.
Rails 3引入了应用程序对象。即使使用Rails 2,您也可能希望以类似的方式存储全局变量。
#4
1
environment.rb is the place. You can add it to a Module and add it to the lib directory. then you could call it as Module::MY_GLOBAL_VARIABLE. Both has strong points and weak points. In the environment.rb is ok but sometimes it looks messy. If all those global variables are related, you can group then in a module.
环境。rb的地方。您可以将其添加到模块并将其添加到lib目录。然后可以将其命名为::MY_GLOBAL_VARIABLE。两者都有优点和缺点。在环境中。rb还可以,但有时看起来很乱。如果所有这些全局变量都是相关的,那么可以在模块中进行分组。
#5
1
I store such configuration information in a YML file. Refer to this screen-cast for more details.
我将这些配置信息存储在YML文件中。有关更多细节,请参阅屏幕强制转换。
Alternatively you can use a gem called app_config.
您也可以使用名为app_config的gem。
#6
1
Normally when I'm doing something like this, I have two ways of doing this
通常当我做这样的事情时,我有两种方法
1 - If my global variables are common to all 3 environments (development, test, production) , then i will store it in
1 -如果我的全局变量对所有3个环境(开发、测试、生产)都是通用的,那么我将它存储在其中
config/environments.rb file
配置/环境。rb文件
But lets say my values are changing upon the environment
但是假设我的价值观随着环境的变化而变化
Ex: my development environment SYSTEM_STORAGE_ROOT is '/development_root/' and testing environment SYSTEM_STORAGE_ROOT is '/testing_root/'
例:我的开发环境SYSTEM_STORAGE_ROOT是'/development_root/',测试环境SYSTEM_STORAGE_ROOT是'/testing_root/'
then I will store them inside
然后我会把它们放在里面
config/environments/
配置/环境/
As per the above example
按照上面的例子。
config/environments/development.rb will have SYSTEM_STORAGE_ROOT = '/development_root/'
配置/环境/发展。rb将具有SYSTEM_STORAGE_ROOT = '/development_root/'
config/environments/test.rb will have SYSTEM_STORAGE_ROOT = '/testing_root/'
配置/环境/测试。rb将具有SYSTEM_STORAGE_ROOT = '/testing_root/'
config/environments/production.rb will have SYSTEM_STORAGE_ROOT = '/production_root/'
配置/环境/生产。rb将具有SYSTEM_STORAGE_ROOT = '/production_root/'
hope this helps,
希望这有助于
cheers, sameera
欢呼,sameera
#1
15
One of my favourite techniques is to put a file containing the constants in directory config/initializers/
(all files in this directory are loaded automatically) but with a section for each different Rails environment. e.g.
我最喜欢的技术之一是在目录配置/初始化器/(这个目录中的所有文件都是自动加载的)中放置一个包含常量的文件,但是每个不同的Rails环境都有一个部分。如。
case ENV['RAILS_ENV']
when "development"
SYSTEM_EMAIL = ...
SYSTEM_EMAIL_SIGNATURE = ...
when "staging"
SYSTEM_EMAIL = ...
SYSTEM_EMAIL_SIGNATURE = ...
when "production"
SYSTEM_EMAIL = ...
SYSTEM_EMAIL_SIGNATURE = ...
end
If you want, instead, to load up all the constants in one big hash then you can load them as a YAML file. Create two files, one called, say, config/initializers/email_constants.rb
and the other config/email_constants.yml
. In the latter put something like:
如果您想要将所有常量加载到一个大的散列中,那么您可以将它们加载为YAML文件。创建两个文件,一个叫做配置/初始化器/email_constant。rb和其他配置/email_constants.yml。后一种说法是:
development:
:system_email: ...
:system_email_signature: ...
staging:
:system_email: ...
system_email_signature: ...
... etc ...
Then in config/initializers/email_constants.rb
put:
然后在config /初始化/ email_constants。rb说:
EMAIL_CONSTANTS = YAML.load_file("#{RAILS_ROOT}/config/email_constants.yml")[RAILS_ENV]
This loads the entire YAML file and the assigns the value of the appropriate key (which represents the RAILS_ENV
) to EMAIL_CONSTANTS
.
这将加载整个YAML文件,并将适当的键(代表RAILS_ENV)的值赋给EMAIL_CONSTANTS。
The advantage of both of these techniques is locality. You can place all the constants that are related to each other (i.e. email constants in this case) in one file. Also, instead of having the same constants spread across three different files (one for each Rails environment) you have them all in one file.
这两种技术的优点都是局部性。您可以将所有相关的常量放在一个文件中(在本例中为电子邮件常量)。而且,与其让相同的常量分布在三个不同的文件(每个Rails环境一个)上,不如将它们全都放在一个文件中。
#2
10
Because such values often change depending on the environment you're running in, I store globals in config/environments/development.rb|production.rb|test.rb
, with appropriate values for each environment.
因为这些值经常根据您所运行的环境而变化,所以我将全局变量存储在config/environments/development.rb|product .rb|测试中。rb,对每个环境都有适当的值。
#3
2
Rails 3 introduces the Application object. Even with Rails 2, you might want to store your globals in a similar manner.
Rails 3引入了应用程序对象。即使使用Rails 2,您也可能希望以类似的方式存储全局变量。
#4
1
environment.rb is the place. You can add it to a Module and add it to the lib directory. then you could call it as Module::MY_GLOBAL_VARIABLE. Both has strong points and weak points. In the environment.rb is ok but sometimes it looks messy. If all those global variables are related, you can group then in a module.
环境。rb的地方。您可以将其添加到模块并将其添加到lib目录。然后可以将其命名为::MY_GLOBAL_VARIABLE。两者都有优点和缺点。在环境中。rb还可以,但有时看起来很乱。如果所有这些全局变量都是相关的,那么可以在模块中进行分组。
#5
1
I store such configuration information in a YML file. Refer to this screen-cast for more details.
我将这些配置信息存储在YML文件中。有关更多细节,请参阅屏幕强制转换。
Alternatively you can use a gem called app_config.
您也可以使用名为app_config的gem。
#6
1
Normally when I'm doing something like this, I have two ways of doing this
通常当我做这样的事情时,我有两种方法
1 - If my global variables are common to all 3 environments (development, test, production) , then i will store it in
1 -如果我的全局变量对所有3个环境(开发、测试、生产)都是通用的,那么我将它存储在其中
config/environments.rb file
配置/环境。rb文件
But lets say my values are changing upon the environment
但是假设我的价值观随着环境的变化而变化
Ex: my development environment SYSTEM_STORAGE_ROOT is '/development_root/' and testing environment SYSTEM_STORAGE_ROOT is '/testing_root/'
例:我的开发环境SYSTEM_STORAGE_ROOT是'/development_root/',测试环境SYSTEM_STORAGE_ROOT是'/testing_root/'
then I will store them inside
然后我会把它们放在里面
config/environments/
配置/环境/
As per the above example
按照上面的例子。
config/environments/development.rb will have SYSTEM_STORAGE_ROOT = '/development_root/'
配置/环境/发展。rb将具有SYSTEM_STORAGE_ROOT = '/development_root/'
config/environments/test.rb will have SYSTEM_STORAGE_ROOT = '/testing_root/'
配置/环境/测试。rb将具有SYSTEM_STORAGE_ROOT = '/testing_root/'
config/environments/production.rb will have SYSTEM_STORAGE_ROOT = '/production_root/'
配置/环境/生产。rb将具有SYSTEM_STORAGE_ROOT = '/production_root/'
hope this helps,
希望这有助于
cheers, sameera
欢呼,sameera