0

以下是条件

users有谁属于一个work,同样category属于一个work

现在我希望该特定作品的用户edit属于该作品的类别。

如何在ability模型中指定它?

编辑 :

include CanCan::Ability

def initialize(user)

user ||= User.new # guest user

user.roles.each do |role|

role.name

if role.name == "admin"

    can :read, User

    can :create, User

    can :edit, User

    can :destroy, User

end

if role.name == "staff"

         can :read, :all

         can :create, :all

        cannot :edit, Category

        can :update, :all

        can :destroy, :all

       end

    if role.name == "others"

          can :read, :all

   end   

end 

end 

end
4

2 回答 2

0

在能力类中,您只传递用户。您还需要通过类别才能做到,

can :manage, Category if user.work_id == category.work_id  

或者你将不得不像这样迭代(我不推荐它),

can :manage, Category if user.work_id == user.work.categories.first.work_id  

此链接显示如何将多个参数发送到您的能力类。

https://github.com/ryanb/cancan/wiki/Accessing-Request-Data

于 2013-03-29T08:48:34.947 回答
0

未经测试,但应该这样做:

can :manage, Category, :work_id => user.work_id

参考:https ://github.com/ryanb/cancan/wiki/defining-abilities

于 2013-03-29T08:09:06.767 回答