我的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,然后只是覆盖我希望为登台环境更改的设置?
答案 0 :(得分:17)
我过去所做的是拥有一个包含共享设置的文件,然后在生产和登台环境文件中要求这样做。这很有效,因为它允许您在一个地方定义常用设置,然后在单个文件中定义唯一设置:
<强>配置/环境/ 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
答案 1 :(得分:2)
我认为这有多好。这些是配置设置,并且可以单独设置。您实际上可以定义一个函数来传递配置。在该功能上,您可以设置默认值,但我不想这样做。在项目的生命周期中,您只能使用少于5个(或10个)的环境,因此您最多需要10个这样的文件,而这些文件不会一直在编辑。