2011-10-18 18 views
2

私はadminブール値フィールドと役割の整数フィールドを持つDeviseで作成されたユーザーモデルを持っています。私は非管理者がロールに基づいて特定の能力を持つことを可能にする能力クラスを作成しようとしていますが、このプロセスに問題があります。私のような権限のコントローラを作成している:私はすべてのページにアクセスしようとするたびcancanとdeviseでユーザーを認証

class AuthorizedController < ApplicationController 
before_filter :authenticate_user! 
check_authorization :unless => :devise_controller? 
load_and_authorize_resource 

rescue_from CanCan::AccessDenied do |exception| 
    flash[:alert] = exception.message 
    redirect_to root_url 
end 

end 

class Ability 
    include CanCan::Ability 

    def initialize(user) 
    if !user 
    can :read, :all 
    end 

    if user 
    admin_rules if user.admin? 
    commenter_rules if user.role.equal?("1") 
    author_rules if user.role.equal?("2") 
    end  
end 

    def admin_rules 
    can :manage, :all 
    end 

    def commenter_rules 
    can :manage, Data, :active => true, :user_id => user.id 
    end 

    def author_rules 
    can :manage, Post, :active => true, :user_id => user.id 
    end 
end 

は今、それは例外メッセージをスロー「このページにアクセスする権限がありません。」ウェブサイトのその部分にアクセスできるのではなく、

+0

は、あなたがエラーを投稿することができますバックトレース? – maxenglander

+0

':active'属性はどこから来ていますか、Devise? – janders223

+0

実際の例外メッセージ –

答えて

1

cancanが正しく動作しています。この方法をデバッグしてみてください。

  1. 一時的にcheck_authorization
  2. を取るあなたが持っているん能力か見て、あなたのビューにいくつかの行を追加します。

    %h1 What abilities do I have? 
    %ul 
        %li can?(:manage, Data) 
        %li can?(:manage, :all) 
        %li etc.. 
    
関連する問題