I’d like to use Rails’s asset pipeline to compile our custom HTTP error pages for nginx. I’d also like to use the standard Rails convention of a layout (say, app/views/layouts/error.html.erb) and views that render within that layout.
I found one article describing a way to simply precompile some ERb templates, but I’d still end up copying most of the layout code between the various templates.
I also thought about using caches_page in a controller and simply forcing the errors to occur during the build so that the files end up in public, but that seems really hacky.
Is there any way to achieve this?
I ended up going the hacky route, which wasn’t as hacky as I thought.
Basically, I created a controller, routes, and templates for the errors I plan to handle:
Then I created a Rake task to “visit” each of these pages, which will cause the controller to cache the page in
/public/errors:Now during deploy, I run this:
to generate our static HTTP error pages.
This will work in any environment where
config.action_controller.perform_caching = true. This is on by default in production, but not in development, so be aware.