Generate static pages in Rails
Posted in Home on July 14th, 2008 by mark – 2 CommentsI wanted a nice looking 404, 500 and maintenance pages for my Rails app and I couldn’t serve them from Rails.
My requirements were:
My solution was to:
Items 1 & 2 are just standard Rails stuff – so go nutz young coders.
Item 3 was pretty straight forward, in the layout I put:
put your sign-in code here
Item 4 was a bit trickier, but this rake task should get you up and running.
namespace :generate do
task :pages => :environment do
require 'action_controller/integration'
app = ActionController::Integration::Session.new
app.host! "stateofflux.com"
[['/errors/error_404', 'public/40 4.html'],
['/errors/error_500', 'public/500.html']].each do |url, file|
begin
app.get url
File.open(file, "w") { |f| f.write app.response.body }
rescue Exception => e
puts "Could not write file #{e}"
end
end
end
end
We run the rake task in the development environment then check it in, but you could run it in production if there was production data that you needed to create the page.