ruby 为cancancan编写能力文件的示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 为cancancan编写能力文件的示例相关的知识,希望对你有一定的参考价值。

class Ability
  include CanCan::Ability

  def initialize(user)

    user ||= User.new

    can :like, Idea do |idea|
      idea.user!=user
    end

    can :join, Idea do |idea|
      idea.user != user
    end

    can :delete,Comment do |comment|
      comment.user==user || comment.idea.user==user
    end

    # Define abilities for the passed in user here. For example:
    #
    #   user ||= User.new # guest user (not logged in)
    #   if user.admin?
    #     can :manage, :all
    #   else
    #     can :read, :all
    #   end
    #
    # The first argument to `can` is the action you are giving the user
    # permission to do.
    # If you pass :manage it will apply to every action. Other common actions
    # here are :read, :create, :update and :destroy.
    #
    # The second argument is the resource the user can perform the action on.
    # If you pass :all it will apply to every resource. Otherwise pass a Ruby
    # class of the resource.
    #
    # The third argument is an optional hash of conditions to further filter the
    # objects.
    # For example, here the user can only update published articles.
    #
    #   can :update, Article, :published => true
    #
    # See the wiki for details:
    # https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
  end
end

以上是关于ruby 为cancancan编写能力文件的示例的主要内容,如果未能解决你的问题,请参考以下文章

ruby CanCanCan可以指定角色

ruby 使用cancancan

基于部门的 Rails CanCanCan 资源能力

Rails、Devise、Role Model 和 CanCanCan - 定义能力

Rails的基于部门CanCanCan资源的能力

Cancancan 通过能力检查传递一个对象