public
Last active

Rake task to generate static error pages in public folder from layouts/exception partial

  • Download Gist
exception.html.erb
HTML+ERB
1 2 3 4 5 6 7 8 9 10 11 12 13
<!DOCTYPE html>
<html>
<head>
<title><%= @status_code %></title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= render "something_else" %>
</head>
<body>
<h1><%= @status_code %></h1>
<p><%= @message %></p>
</body>
</html>
exceptions.rake
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
# Rake task to generate static error pages in public folder from layouts/exception partial
# Usage: RAILS_ENV=production rake exceptions:compile_error_pages
 
class MockView < ActionView::Base
include Rails.application.routes.url_helpers
include ActionView::Helpers::TagHelper
end
class MockController < ActionController::Base
def initialize
super
self.request = ActionDispatch::TestRequest.new
end
end
namespace :exceptions do
EXCEPTIONS = {
500 => 'Internal Server Error!'
}
desc 'Compile static error pages'
task :compile_error_pages => :environment do
template = MockView.new Rails.root.join('app', 'views'), {}, MockController.new
EXCEPTIONS.each do |status_code, message|
file_name = Rails.root.join('public', "#{status_code}.html")
print "Compiling '#{file_name}'... "
begin
template.assign status_code: status_code, message: message
content = template.render layout: nil, file: 'layouts/exception'
File.open file_name, 'w' do |f|
f.write content
end
puts 'OK'
rescue Exception => e
puts 'FAILED'
raise
end
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.