将envionrment变量插入配置文件的正确方法是什么?

时间:2017-05-29 18:05:49

标签: configuration environment-variables ruby-on-rails-5 sidekiq

使用Rails 5.0.1。我在config / initializers / sidekiq.rb文件中有这个...

Sidekiq.configure_server do |config|
  config.redis = { url: 'redis://#{ENV['REDIS_PORT_6379_TCP_ADDR']}:#{ENV['REDIS_PORT_6379_TCP_PORT']}/12', namespace: "sidekiq_app_name_#{ENV['RAILS_ENV']}" }
end

Sidekiq.configure_client do |config|
  config.redis = { url: 'redis://#{ENV['REDIS_PORT_6379_TCP_ADDR']}:#{ENV['REDIS_PORT_6379_TCP_PORT']}/12', namespace: "sidekiq_app_name_#{ENV['RAILS_ENV']}" }
end

虽然我的系统中定义了envionrment变量,但当我尝试启动服务器时,我收到了这些错误...

/Users/davea/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.2/lib/active_support/dependencies.rb:287:in `load': /Users/davea/Documents/workspace/myproject/config/initializers/sidekiq.rb:2: syntax error, unexpected tCONSTANT, expecting '}' (SyntaxError)
{ENV['REDIS_PORT_6379_TCP_ADDR']}:#{ENV['REDIS_PORT_6379_TCP
                              ^
/Users/davea/Documents/workspace/myproject/config/initializers/sidekiq.rb:2: syntax error, unexpected tCONSTANT, expecting keyword_end
{ENV['REDIS_PORT_6379_TCP_PORT']}/12', namespace: "sidekiq_a
                              ^
/Users/davea/Documents/workspace/myproject/config/initializers/sidekiq.rb:2: syntax error, unexpected '}', expecting end-of-input
pp_name_#{ENV['RAILS_ENV']}" }

将环境变量插入配置文件的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

你可以使用宝石figaro:

gem "figaro"

如何使用它,在readme.txt解释非常简单:

config/application.yml

stripe_api_key: "sk_live_dSqzdUq80sw9GWmuoI0qJ9rL"



ENV["stripe_api_key"] # => "sk_live_dSqzdUq80sw9GWmuoI0qJ9rL"
ENV.key?("stripe_api_key") # => true
ENV["google_analytics_key"] # => nil
ENV.key?("google_analytics_key") # => false

Figaro.env.stripe_api_key # => "sk_live_dSqzdUq80sw9GWmuoI0qJ9rL"
Figaro.env.stripe_api_key? # => true
Figaro.env.google_analytics_key # => nil
Figaro.env.google_analytics_key? # => false