我最终走上了哈克路线,这不像我想象的那样哈克。
基本上,我创建了一个控制器,路线,并针对这些错误的模板我打算处理:
# config/routes.rb
resources :errors, only: :show
# app/controllers/errors_controller.rb
class ErrorsController < ApplicationController
layout 'errors'
# you may need to disable various filters
skip_before_filter :authenticate_user!
# cache full versions of the pages we generate
caches_page :show
def show
render action: params[:id]
end
end
# app/views/errors/404.html.erb and so on
404 Not Found
然后,我创建了一个Rake任务“拜访”在这些页面中,这将导致控制器缓存的页面中/public/errors:
task :create_error_pages => :environment do
session = ActionDispatch::Integration::Session.new(Rails.application)
%w{401 404 422 ...}.each do |error|
session.get("/errors/#{error}")
end
end 部署期间
现在,我运行此:
RAILS_ENV=production bundle exec rake assets:precompile create_error_pages
来生成我们的静态HTTP错误页面。
这将工作在任何环境config.action_controller.perform_caching = true。这是默认情况下在生产中,但不是在开发中,所以要注意。