当我们在开发模式下返回丢失的图像时如何捕获ActionController :: RoutingError?

时间:2015-12-26 14:47:15

标签: ruby-on-rails ruby

如何在开发模式下返回丢失的图像时捕获ActionController :: RoutingError? 当我运行Rspec和Capybara测试时,很难发现这个错误。

1 个答案:

答案 0 :(得分:-1)

捕捉所有例外情况: 将其添加到application_controller,

  rescue_from ActiveRecord::RecordNotFound, :with => :render_404
  rescue_from ActiveRecord::RecordInvalid, :with => :render_500
  rescue_from ActionController::MissingFile, :with => :render_404
  rescue_from ActionController::RoutingError, :with => :render_404
  rescue_from ActionView::TemplateError, :with => :render_500
  rescue_from Errno::ENOENT, :with => :render_404
  rescue_from Errno::EACCES, :with => :render_404

  def render_404
   if Rails.env.production?  
    render :layout=>false,:file => Rails.root.join('public', '404.html'), :status => 404
   else
    raise Exception, I18n.t('str_error')+" - #{error.inspect}"
    #comment the above line and uncomment the below line if you want to custom see error pages instead of error messages in your local
    #render :layout=>false,:file => Rails.root.join('public', '404.html'), :status => 404
   end
  end