在 application_controller.rb 中有一种方法可以使用自定义布局和部分呈现 404 错误。
application_controller.rb:
unless Rails.application.config.consider_all_requests_local
rescue_from Exception, with: :render_500
rescue_from ActionController::RoutingError, with: :render_404
rescue_from ActionController::UnknownController, with: :render_404
rescue_from ActionController::UnknownAction, with: :render_404
rescue_from ActiveRecord::RecordNotFound, with: :render_404
end
这会调用任何方法:
def render_404(exception)
notify exception
@not_found_path = exception.message
respond_to do |format|
format.html { render template: 'errors/error_404', layout: 'layouts/error', status: 404 }
format.all { render nothing: true, status: 404 }
end
end
def render_500(exception)
notify exception
@error = exception
respond_to do |format|
format.html { render template: 'errors/error_404', layout: 'layouts/error', status: 404 }
format.all { render nothing: true, status: 500 }
end
end
然后是error_controller.rb:
class ErrorsController < ApplicationController
layout "error"
def sub_layout
"left"
end
def error_404
@not_found_path = params[:not_found]
render "errors/error_404"
end
def error_500
render "errors/error_500"
end
end
问题:这感觉很臃肿,有没有办法让它呈现现在位于 /app/views/errors/error_404.html.haml 的自定义错误页面?与自定义布局?
我想删除我的 error_controller.rb