本文介绍了自定义错误处理和cancan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现自定义错误处理以及使用CanCan。当用户到达某个区域时,它们不允许转到,CanCan :: AccessDenied错误将被抛出,并将它们发送到根URL。相反,rescue_from Exception捕获CanCan :: AccessDenied,并且用户获得500错误。我做错了什么?

 #application_controller.rb 
rescue_from CanCan :: AccessDenied do | exception |
redirect_to main_app.root_url,:alert => exception.message
end

rescue_from异常,
:with => :render_error
rescue_from Mongoid :: Errors :: DocumentNotFound,
:with => :render_not_found
rescue_from ActionController :: RoutingError,
:with => :render_not_found
rescue_from ActionController :: UnknownController,
:with => :render_not_found
rescue_from AbstractController :: ActionNotFound,
:with => :render_not_found


def render_not_found(exception)
render:template => /errors/404.html,
:layout => 'errors.html',
:status => 404
end

def render_error(exception)
render:template => /errors/500.html,
:layout => 'errors.html',
:status => 500
end


解决方案

你尝试重新排序rescue_from例外/错误,更通用的第一个,稍后更具体,例如

  rescue_from StandardError,
:with => :render_error
rescue_from Mongoid :: Errors :: DocumentNotFound,
:with => :render_not_found
rescue_from ActionController :: RoutingError,
:with => :render_not_found
rescue_from ActionController :: UnknownController,
:with => :render_not_found
rescue_from AbstractController :: ActionNotFound,
:with => :render_not_found
rescue_from CanCan :: AccessDenied do | exception |
redirect_to main_app.root_url,:alert => exception.message
end

注意:您可能希望将普通异常替换为StandardError。 / p>

I am trying to implement custom error handling as well as use CanCan. When a user reaches an area they aren't allowed to go to, a CanCan::AccessDenied error is thrown and they should be sent to the root url. Instead, 'rescue_from Exception' captures the CanCan::AccessDenied and the user gets a 500 error. What am I doing wrong?

#application_controller.rb
rescue_from CanCan::AccessDenied do |exception|
  redirect_to main_app.root_url, :alert => exception.message
end

rescue_from Exception,
  :with => :render_error
rescue_from Mongoid::Errors::DocumentNotFound,
  :with => :render_not_found
rescue_from ActionController::RoutingError,
  :with => :render_not_found
rescue_from ActionController::UnknownController,
  :with => :render_not_found
rescue_from AbstractController::ActionNotFound,
  :with => :render_not_found


def render_not_found(exception)
  render :template => "/errors/404.html",
       :layout => 'errors.html',
       :status => 404
end

def render_error(exception)
  render :template => "/errors/500.html",
       :layout => 'errors.html',
       :status => 500
end
解决方案

Did you try reordering rescue_from exceptions/errors, more generic first, more specific later, e.g.

rescue_from StandardError,
  :with => :render_error
rescue_from Mongoid::Errors::DocumentNotFound,
  :with => :render_not_found
rescue_from ActionController::RoutingError,
  :with => :render_not_found
rescue_from ActionController::UnknownController,
  :with => :render_not_found
rescue_from AbstractController::ActionNotFound,
  :with => :render_not_found
rescue_from CanCan::AccessDenied do |exception|
  redirect_to main_app.root_url, :alert => exception.message
end

Note: You might want to replace generic Exception with StandardError.

这篇关于自定义错误处理和cancan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 10:56