3

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 requests in the HTML format.

However, when someone provides a URL indicating a non-HTML format, e.g. mysite.com/missingfile.png this throws a 500 error becauses I don't have a 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.

A key point is that this is a dynamic 404 page, so the usual public/404.html route does not work for me.

Thanks!

4

1 に答える 1

1

これを行う ...

#config/routes
get "*unmatched_route", :to => "application#error_page"


#controllers/application_controller
def error_page
  render :template => 'errors/error_404', status: 404
end
于 2015-08-08T15:08:49.467 に答える