2017-05-26 1 views
-1

私はこのようになりますRailsアプリケーションでの基本的な承認クラスがあります。今とは何ですか:manage、:すべてRubyでやっていますか?

class Ability 
    include CanCan::Ability 

    def initialize(user) 

    if user 
    can :access, :rails_admin  # only allow admin users to access Rails Admin 
    can :dashboard 
    if user.admin? 
     can :manage, :all 
    else 
     can :manage, [Agreement, Attachment, Contact, Deadline, Event, Image, Photo, Project, Submission, Talk] 
     can :update, User, id: user.id 
    end 
    end 

    # Current user cannot delete his account 
    cannot :destroy, User, id: user.id 
    end 
end 

を、簡単なユーザーとダッシュボードにアクセスしようとしたとき、私は不正なエラーを取得するが、私はシンプルなためcan :manage, :allを入れたら、ユーザーの状態それはmisteriousllyスルーし、ダッシュボードを参照してください。

:manage, :all:manage, [All_my_tables]を超えていますが、なぜ私のユーザーはこの方法で使用できないのですか?

+3

** HTTPSを来る前に研究されている://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities** –

+0

ありがとうございました私はwikiを共有していますが、私はそのページをすでに見てきました。そこに私の答えが見つかると、私はここに質問を投稿しません –

+0

* "ダッシュボードにアクセスしようとすると不正なエラーが発生します" * - これはですか?あなたが 'can:read、:dashboard'の代わりに' can:dashboard'を書いたからではありませんか?そうでない場合は、ユーザーがどのような行為を許可されていないかについて具体的に説明できますか? (コントローラには何が入っていますか?) –

答えて

0

これは答えです。単純なユーザーの場合は:manage, :allにして、アクセス許可を上書きする必要があります。

class Ability 
    include CanCan::Ability 

    def initialize(user) 

    #Check if the user is logged in 
    if user 
     #Grant access to the dashboard 
     can :access, :rails_admin 
     can :dashboard 
     can :manage, :all 

     #Simple user permissions set here 
     if !user.admin? 
     alias_action :create, :update, :destroy, to: :cud 

     can :manage, :all 
     cannot :cud, User 
     cannot :destroy, [Agreement, Submission] 
     end 
    end 

    can :update, User, id: user.id  #User can edit his/her own account 
    cannot :destroy, User, id: user.id #User cannot delete his/her own account 
    end 
end 

ダウン票のおかげで、この質問はここにも

関連する問題