user.rb
def has_delete_role? name
roles.each do |n|
return true if n == name
end
endability.rb
if user.has_delete_role? :business_delete
can :destroy, Business
endindex.html.erb
<% if can? :destroy, @business %>
<%= link_to 'delete', business_path(@business.id), method: :delete%>
<% end %>这段代码允许有权访问删除按钮的用户。在这里,如果用户有权限,他可以访问所有对象的删除按钮。
例:业务有10个对象id =1到id = 10,如果用户有权限,可以访问所有10个删除按钮。
但是现在我想基于对象来设置权限。
例: Buisness也是1到10,用户只能看到按钮2和5,因为在用户数据表中有一个名为auth_ids []的字段,它存储2,5
如何做到这一点?
发布于 2016-12-22 05:00:42
您可以使用:
can :destroy, Business, id: user.auth_ids发布于 2016-12-22 04:36:38
您可以设置一个条件,类似于这里的指南:https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
can :destroy, Business, Business.where('id = ?', user.auth_ids)https://stackoverflow.com/questions/41275549
复制相似问题