【发布时间】:2014-10-23 09:11:22
【问题描述】:
我在同一个应用程序(但不同的控制器和命名空间)中有一个 API 作为标准视图和控制器来处理相同的资源。
目前,当我收到 404 时,无论请求是以 HTML 还是 json 格式发出的,我都会收到 HTML 404 响应。
应用程序控制器部分处理 404 如下:
unless Rails.application.config.consider_all_requests_local
rescue_from ActiveRecord::RecordNotFound, with: :render_denied_404
rescue_from ActionController::RoutingError, with: :render_404
rescue_from ::AbstractController::ActionNotFound, with: :render_404
rescue_from ActionController::UnknownController, with: :render_404
rescue_from ActionView::MissingTemplate, with: :render_404
end
def render_404(e = Exception.new)
Rails.logger.info "Rendering 404: #{e.to_s}"
flash[:error] = "Error 404 " + e.to_s
redirect_to "/404"
end
如何更改此设置以根据请求格式类型以不同方式处理 404 错误?
【问题讨论】: