gpt4 book ai didi

ruby-on-rails - 删除 staging.rb 和 production.rb 之间的重复

转载 作者:数据小太阳 更新时间:2023-10-29 06:41:09 30 4
gpt4 key购买 nike

我的 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,然后覆盖我希望为暂存环境更改的设置?

最佳答案

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

config/environments/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

config/environments/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

config/environments/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

关于ruby-on-rails - 删除 staging.rb 和 production.rb 之间的重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14947833/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com