cancancan自定义操作不起作用
问题描述:
我在我的应用程序中使用cancancan和activeadmin宝石,在cancan gem自定义操作不起作用。cancancan自定义操作不起作用
ability.rb
if ((user.has_role? :HRMS_Supervisor) && (user.has_application? :HRMS))
can :manage, User
can :approve, User // custom action
end
if ((user.has_role? :HRMS_Employee) && (user.has_application? :HRMS))
can :read, Employee
can :manage, User
can :employee_access, User // custom action
end
我activeadmin文件
ActiveAdmin.register Teleworker do
scope :pending, default: true
scope :approved
scope :rejected, if: proc{ can? :employee_access, current_user }
scope :all
index do
selectable_column
column "Action" do |resource|
links = ''.html_safe
if can? :approve, current_user
links += link_to "approve", resource_path(resource), class: "member_link view_link"
end
end
end
被拒绝的范围和的link_to “批准” 显示的是两个角色。如何解决这个问题。
答
can :manage, User
已包含所有自定义操作。所以,你的角色都可以执行两个自定义操作。
对于这两个角色,您只能描述crud动作:can %i(create read update delete), User
而不是can :manage, User
。
...并且您还需要授权您的自定义操作:https://github.com/CanCanCommunity/cancancan/wiki/Authorizing-controller-actions – nistvan 2014-12-04 09:08:15
非常感谢它的作品。 – 2014-12-04 09:10:44