14

我的 Rails 应用程序(在 Heroku 上运行)有一个登台和生产环境。目前,staging.rb 和 production.rb 中有很多东西我必须在每个文件中单独定义,例如:

  # Code is not reloaded between requests
  config.cache_classes = true

  # Full error reports are disabled and caching is turned on
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Disable Rails's static asset server (Apache or nginx will already do this)
  config.serve_static_assets = false

  # Compress JavaScripts and CSS
  config.assets.compress = true

  # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.compile = false

  # Generate digests for assets URLs
  config.assets.digest = true

这不是DRY。有没有一种优雅的方法可以有效地将 production.rb 中的设置导入到 staging.rb 中,然后只覆盖我希望为暂存环境更改的设置?

4

2 回答 2

19

我过去所做的是有一个包含共享设置的文件,然后在生产和登台环境文件中需要它。这很有效,因为它允许您在一个地方定义通用设置,然后在各个文件中定义唯一设置:

配置/环境/shared_production.rb

MyApp::Application.configure do
  # Code is not reloaded between requests
  config.cache_classes = true

  # Full error reports are disabled and caching is turned on
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true
end

配置/环境/production.rb

require Rails.root.join('config/environments/shared_production')

MyApp::Application.configure do
  # Raise exception on mass assignment protection for Active Record models
  config.active_record.mass_assignment_sanitizer = :logger

  # Url to be used in mailer links
  config.action_mailer.default_url_options = { :host => "production.com" }
end

配置/环境/staging.rb

require Rails.root.join('config/environments/shared_production')

MyApp::Application.configure do
  # Raise exception on mass assignment protection for Active Record models
  config.active_record.mass_assignment_sanitizer = :strict

  # Url to be used in mailer links
  config.action_mailer.default_url_options = { :host => "mysite-dev.com" }
end
于 2013-02-19T01:39:16.557 回答
2

我认为这就是它变得多么好。这些是配置设置,旨在单独设置。您实际上可以定义一个函数来传递配置。在该功能上,您可以设置默认值,但我不想这样做。在项目的生命周期中,您只使用少于 5 个(或 10 个)环境,因此您最多需要 10 个这些文件,您不会一直在编辑这些文件。

于 2013-02-19T01:27:41.783 回答