【问题标题】:Rails 4.2.5 - Unable to upgrade "config.serve_static_files" to "config.public_file_server.enabled"Rails 4.2.5 - 无法将“config.serve_static_files”升级到“config.public_file_server.enabled”
【发布时间】:2016-03-11 15:07:54
【问题描述】:

当我使用新的 Rails 5.1 方法“public_file_server.enabled”配置我的 production.rb 文件时,我无法预编译我的资产然后部署我的应用程序(在 Heroku 上)。

这是我在运行“RAILS_ENV=production bundle exec rake assets:precompile”后看到的错误:

rake aborted!
NoMethodError: undefined method `public_file_server' for #<Rails::Application::Configuration:0x007f81f0624b60>
/Users/jmarbach/Documents/rubyscripts/concorde/config/environments/production.rb:21:in `block in <top (required)>'
/Users/jmarbach/Documents/rubyscripts/concorde/config/environments/production.rb:1:in `<top (required)>'
/Users/jmarbach/Documents/rubyscripts/concorde/config/environment.rb:5:in `<top (required)>'

这是我的 production.rb 文件中的相关行:

# Leverage browser caching
config.static_cache_control = 'public, max-age=1000'

# Set header expiray date
config.public_file_server.enabled = true
config.public_file_server.headers = {
  'Cache-Control' => 'public, s-maxage=31536000, maxage=15552000',
  'Expires' => "#{1.year.from_now.to_formatted_s(:rfc822)}"
  }

这是我的 gem 文件中的 rails 配置:

gem 'rails', '4.2.5'
gem 'railties', '4.2.5'

如何访问新合并的方法“public_file_server”?

【问题讨论】:

  • 导致我的应用程序崩溃的相关问题:stackoverflow.com/questions/26289414/… > 我解决了这个问题,至少在我的情况下。要解决这个问题,运行 > > rake assets:precompile > > 此时我提交了我的更改,然后终于能够: > > git push heroku master
  • 这是运行 'Heroku run rails console' 的结果:method_missing': undefined method public_file_server' for #<:application::configuration:0x007f6b998e3030> (NoMethodError)

标签: ruby-on-rails caching


【解决方案1】:

我已经尝试了所有属于 Ruby on Rails 的东西,但决定跳到 nginx 配置。以下是帮助我利用浏览器缓存的方法:

location ~* \.(?:jpg|jpeg|gif|png|ico|gz|js|css|ttf)$ {
    expires 1M;
    access_log off;
    add_header Cache-Control "public";
}

希望它可以帮助任何人:)

【讨论】:

    【解决方案2】:

    正如您在问题中指出的那样,“public_file_server”是 Rails 5.0 功能。如您的 gemfile 所示,您正在运行 rails 4.2.5。您需要升级到 Rails 5.0。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 2019-02-26
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多