Rails Controller - Handles 404s and responds with a dynamic HTML error page regardless of the request format
Suppose I have the following logic in my ApplicationController:
rescue_from ActionController::RoutingError, :with => :handle_routing_error
def handle_routing_error(exception)
logger.info { "handling routing error: #{exception.message}" }
render template: 'errors/error_404', status: 404
end
This renders my custom (and dynamic) 404 error page for all HTML requests.
However, when someone provides a url that specifies a non-HTML format, eg. mysite.com/missingfile.png this throws a 500 error because I don't have the error_404.png template:
Missing template errors/error_404, public_site/error_404, application/error_404 with {:locale=>[:en], :formats=>[:png], :handlers=>[:erb, :builder, :coffee, :rabl, :haml]}. Searched in:
How can I override the request format and always show my dynamic HTML 404 page? I want it to work like twitter: https://twitter.com/missingfile.png .
The key point is that this is a dynamic 404 page, so the normal public / 404.html route doesn't work for me.
Thank!
+3
source to share