2014-12-08 9 views
12

CanCanCan, Devise & Rolify를 사용하고 있습니다.CanCanCan이 지정된 것처럼 플래시 메시지가 아닌 예외에 일반 레일 오류를 발생시킵니다.

ApplicationController은 다음과 같습니다

class ApplicationController < ActionController::Base 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 
    before_filter :new_post 

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

    def new_post 
    @post = Post.new 
    end 

end 

routes.rb은 다음과 같습니다

elsif user.has_role? :member 
    can :read, :all 
    cannot :read, :newsroom 

그래서 내가 로그인 한 경우 :

authenticate :user, lambda { |u| u.has_role? :admin or :editor } do 
    get 'newsroom', to: 'newsroom#index', as: "newsroom" 
    get 'newsroom/published', to: 'newsroom#published' 
    get 'newsroom/unpublished', to: 'newsroom#unpublished'  
    end 

# truncated for brevity 
    root to: "posts#index" 

이 관련 내 ability.rb입니다 안으로 :member, 그리고 나는에 간다 내가 기대 한 것처럼

NameError at /newsroom 
uninitialized constant Newsroom 

보다는

:alertroot_url로 리디렉션되는 : /newsroom, 나는이 오류가 발생합니다.

여기 무슨 일인지 잘 모르겠습니다.

NewsroomController이를 추가 할 때 편집 한 그것은 가치가 무엇인지에 대한

, 나는 단지이 얻을 :

authorize_resource 
+1

이 오류를 유발 하는가 – Typpex

+0

@Typpex 예는 d를 oes는 admin으로 로그인 한'/ newsroom'에 접근하면 에러를 발생시킵니다. – marcamillion

답변

2

그것은 문제가 내 Newsroom 컨트롤러를 승인 방법을 것을 밝혀 - Newsroom가 아닌 편안한 컨트롤러 이었기 때문에 (즉 Newsroom와 관련된 모델이 없었다

내가 정상에 이것을 추가했다. 여기에 지정된으로

authorize_resource :class => false 

: 내 컨트롤러의? 당신이 관리자로 loggued/뉴스 룸에 액세스하는 경우 https://github.com/CanCanCommunity/cancancan/wiki/Non-RESTful-Controllers#alternative-authorize_resource

2

내가 매우 캉캉 경험이 아니에요하지만 난 당신을 볼 수 있습니다 예외는 NameError 인 반면 CanCan :: AccessDenied에서 구해냅니다. 따라서 구조 블록 작동을 기대해서는 안됩니다.

내가 'Newsroom'을 어딘가에서 잘못 입력했거나 사용할 수없는 곳에서 액세스하고 있다고합니다.

+0

문서는 예외 처리를 위해 CanCan :: AccessDenied에서 구해야한다고 말합니다 - https://github.com/CanCanCommunity/cancancan/wiki/Exception-Handling – marcamillion