Skip to content

Instantly share code, notes, and snippets.

@tylerhunt
Created September 10, 2014 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerhunt/c93d26673e25c0474ef2 to your computer and use it in GitHub Desktop.
Save tylerhunt/c93d26673e25c0474ef2 to your computer and use it in GitHub Desktop.
Static error pages from dynamically generated assets.

The guide Asset Pipeline Error Pages now no longer works, since non-digest assets aren't generated in newer Rails apps. To fix this, I've replaced the config/initializers/exceptions.rb initializer that swaps out the middleware with the Rake task here, which will be run after Rails assets:precompile task.

namespace :assets do
task :precompile do
# copy generated HTML error pages into public path after asset generation
%w(404.html 422.html 500.html).each do |file|
asset_file = ActionController::Base.helpers.asset_digest_path(file)
asset_path = Rails.public_path.join('assets', asset_file)
public_path = Rails.public_path.join(file)
cp asset_path, public_path
end
end
end
@zealot128
Copy link

Very nice approach, too.
I've taken the base idea but used a different approach:

Rails 5+ has the ApplicationController.renderer, which can render any template at any time. This works better than the Asset Pipeline, because the view works almost exactly the same. I've build a small gem, that wraps that:

https://rubygems.org/gems/static_error_pages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment