假设我的 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
这将为 HTML 格式的所有请求呈现我的自定义(和动态)404 错误页面。
但是,当有人提供指示非 HTML 格式的 URL 时,例如 mysite.com/missingfile.png 这会引发 500 错误,因为我没有 error_404.png 模板:
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:
如何覆盖请求格式并始终显示我的动态 HTML 404 页面?我希望它像 twitter 一样工作:https ://twitter.com/missingfile.png 。
关键是这是一个动态的 404 页面,所以通常的 public/404.html 路由对我不起作用。
谢谢!