I have the following code in /config/initializers/chargify.rb
我在/config/initializers/chargify.rb中有以下代码
Chargify.configure do |c|
c.subdomain = 'example'
c.api_key = '123xyz'
end
But I have different settings for development and production.
但是我有不同的开发和生产环境。
So, how would I have a different set of variables values based on environment?
那么,如何根据环境得到一组不同的变量值呢?
4 个解决方案
#1
29
I would create a config file for this (config/chargify.yml
):
我将为此创建一个配置文件(config/chargify.yml):
development:
subdomain: example
api_key: 123abc
production:
subdomain: production_domain
api_key: 890xyz
And then change your Initializer like this:
然后像这样改变初始化器:
chargify_config_file = File.join(Rails.root,'config','chargify.yml')
raise "#{chargify_config_file} is missing!" unless File.exists? chargify_config_file
chargify_config = YAML.load_file(chargify_config_file)[Rails.env].symbolize_keys
Chargify.configure do |c|
c.subdomain = chargify_config[:subdomain]
c.api_key = chargify_config[:api_key]
end
#2
5
What about:
是什么:
Chargify.configure do |c|
if Rails.env.production?
# production settings
c.api_key = '123xyz'
else
# dev. and test settings
c.api_key = '123xyz'
end
end
Better yet, you can reduce duplication with case
block:
更好的是,您可以使用case block减少重复:
Chargify.configure do |c|
c.subdomain = 'example'
c.api_key = case
when Rails.env.production?
'123xyz'
when Rails.env.staging?
'abc123'
else
'xyz123'
end
end
#3
4
If you're going to need different settings for different environments, it's best to put them in the respective environment file, like config/environments/development.rb
.
如果您需要针对不同的环境进行不同的设置,最好将它们放在相应的环境文件中,如config/environment /development.rb。
If you absolutely insist on putting them in an initializer (but please don't, that's what the environment files are for), you can use a case
statement and inspect the value of Rails.env
, which returns the name of the current environment as a string.
如果您绝对坚持将它们放在初始化器中(但请不要这样做,这就是环境文件所要做的),您可以使用case语句并检查Rails的值。env,它以字符串的形式返回当前环境的名称。
#4
0
I would suggest you to use env variables
我建议你使用env变量
Chargify.configure do |c|
c.subdomain = ENV['APP_SUBDOMAIN']
c.api_key = ENV['API_KEY']
end
and set appropriate variables in ~/.bashrc or ~/.profile but note: this must be set for same user as Rails instance is opareting on. E.g. deploy user specified in capistrano is you used that for deployments
并在~/中设置适当的变量。bashrc或(~ /。配置文件但注意:这必须设置为与Rails实例相同的用户。在capistrano中指定的部署用户是用于部署的。
#1
29
I would create a config file for this (config/chargify.yml
):
我将为此创建一个配置文件(config/chargify.yml):
development:
subdomain: example
api_key: 123abc
production:
subdomain: production_domain
api_key: 890xyz
And then change your Initializer like this:
然后像这样改变初始化器:
chargify_config_file = File.join(Rails.root,'config','chargify.yml')
raise "#{chargify_config_file} is missing!" unless File.exists? chargify_config_file
chargify_config = YAML.load_file(chargify_config_file)[Rails.env].symbolize_keys
Chargify.configure do |c|
c.subdomain = chargify_config[:subdomain]
c.api_key = chargify_config[:api_key]
end
#2
5
What about:
是什么:
Chargify.configure do |c|
if Rails.env.production?
# production settings
c.api_key = '123xyz'
else
# dev. and test settings
c.api_key = '123xyz'
end
end
Better yet, you can reduce duplication with case
block:
更好的是,您可以使用case block减少重复:
Chargify.configure do |c|
c.subdomain = 'example'
c.api_key = case
when Rails.env.production?
'123xyz'
when Rails.env.staging?
'abc123'
else
'xyz123'
end
end
#3
4
If you're going to need different settings for different environments, it's best to put them in the respective environment file, like config/environments/development.rb
.
如果您需要针对不同的环境进行不同的设置,最好将它们放在相应的环境文件中,如config/environment /development.rb。
If you absolutely insist on putting them in an initializer (but please don't, that's what the environment files are for), you can use a case
statement and inspect the value of Rails.env
, which returns the name of the current environment as a string.
如果您绝对坚持将它们放在初始化器中(但请不要这样做,这就是环境文件所要做的),您可以使用case语句并检查Rails的值。env,它以字符串的形式返回当前环境的名称。
#4
0
I would suggest you to use env variables
我建议你使用env变量
Chargify.configure do |c|
c.subdomain = ENV['APP_SUBDOMAIN']
c.api_key = ENV['API_KEY']
end
and set appropriate variables in ~/.bashrc or ~/.profile but note: this must be set for same user as Rails instance is opareting on. E.g. deploy user specified in capistrano is you used that for deployments
并在~/中设置适当的变量。bashrc或(~ /。配置文件但注意:这必须设置为与Rails实例相同的用户。在capistrano中指定的部署用户是用于部署的。