您好我已经写了这段代码
class Ability
include CanCan::Ability
def initialize(employee)
employee ||= Employee.new
if employee[:role]== 'SUPER-ADMIN'
can :manage, :all
elsif employee[:role]== 'HR'
can :manage, :Employee
can :manage, :Interview
elsif employee[:role]== 'INVENTORY'
can :manage, :Inventory
else
can :read, :all
end
end
end
现在我必须给出switch case而不是if else条件这样的
case role
when "SUPER-ADMIN"
can :manage, :all
when "HR"
can :manage, :Employee
can :manage, :Interview
when "INVENTORY"
can :manage, :Inventory
when "Employee"
can :read, :all
end
请指导我如何做到这一点。提前致谢
答案 0 :(得分:1)
你快到了。您可以对case
进行一些小改动以使其正常工作:
class Ability
include CanCan::Ability
def initialize(employee)
employee ||= Employee.new
case employee[:role]
when 'SUPER-ADMIN'
can :manage, :all
when 'HR'
can :manage, :Employee
can :manage, :Interview
when 'INVENTORY'
can :manage, :Inventory
else
can :read, :all
end
end
end