简体   繁体   English

rails自定义错误异常的实现肿,建议将其枯竭吗?

[英]rails custom error exception implementation is bloated, suggestions for drying it up?

In application_controller.rb there is a method to render 404 errors with a custom layout and partial. 在application_controller.rb中,有一种使用自定义布局和部分布局呈现404错误的方法。

application_controller.rb: 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

This calls any of the methods: 这将调用以下任何方法:

  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

Then there is error_controller.rb: 然后是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

QUESTION: This feels very bloated, Is there a way to have it render the custom error page now situated in /app/views/errors/error_404.html.haml ? 问题:感觉很肿,有没有办法让它呈现现在位于/app/views/errors/error_404.html.haml中的自定义错误页面? with custom layout? 自定义布局?

I want to delete my error_controller.rb 我想删除我的error_controller.rb

The default path for rendering 404 and 500 html responses is in the public folder. 呈现404和500 html响应的默认路径在公用文件夹中。 I'd replace the default 404.html with your error_404.html 我会用您的error_404.html替换默认的404.html

render :file => 'public/404.html', :layout => layouts/error

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM