2013-11-20 5 views
3

사용자 지정 오류 처리를 구현하고 CanCan을 사용하려고합니다. 사용자가 이동할 수없는 영역에 도달하면 CanCan :: AccessDenied 오류가 발생하고 루트 URL로 보내야합니다. 대신 'rescue_from Exception'은 CanCan :: AccessDenied를 캡처하고 사용자는 500 오류를 얻습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?사용자 정의 오류 처리 및 Cancan

#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 

답변

0

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로 바꿀 수도 있습니다.

+0

이 솔루션은 저에게 효과적이었습니다. 레일즈 문서는 당신이 지적한대로 주문이 중요하다는 것을 분명히합니다. –