0

我的生产和开发环境都有以下配置。

config.action_mailer.smtp_settings = {
  :address   => "smtp.mandrillapp.com",
  :port      => 587,
  :user_name => ENV["MANDRILL_USERNAME"],
  :password  => ENV["MANDRILL_PASSWORD"]
}

我用谷歌搜索了一段时间,但没有找到让我理解的答案。如何为我的 Rails 项目设置用于开发和生产的 ENV 变量?

4

2 回答 2

2

For development, you'll want to use a configuration file that is ignored in version control. It could be YAML, JSON, ruby, bash... it's a trivial choice. You'll then use an initializer file to make sure these variables are loaded when the app boots. For production, you can copy the config file (it may even have different settings) into the right place as part of your deploy process; or if you're using heroku, you can set these from command line using heroku config:add YADA=yada.

Here's a basic example with settings coming from a YAML file:

# config/settings.yml
development:
 MANDRILL_USERNAME=secret_username
 MANDRILL_PASSWORD=secret_password
 ANOTHER_SECRET_SETTING=the_list_goes_on_and_on
production:
 MANDRILL_USERNAME=different_username
 MANDRILL_PASSWORD=another_password
 ANOTHER_SECRET_SETTING=get_the_idea?

# .gitigore
# ...
config/settings.yml

# config/initializers/environment_settings.rb
environment_settings = YAML.load_file('./config/settings.yml')[Rails.env]
environment_settings.each do |key, value|
  ENV[key] ||= value
end
于 2012-09-28T02:29:18.730 回答
1

它们是环境变量;您不会将它们设置为开发和生产。

您可以通过多种方式设置应用程序配置变量,或者您可以在运行前在 shell 中设置它们,或者您可以使用特定于环境的名称,或者...

于 2012-09-27T19:38:05.027 回答