问题描述
|
我正在为我的应用程序使用cancan
我的Capacity.rb类是
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user
if user.role? :admin
can :manage,:all
elsif user.role? :operations
can :manage,:all
elsif user.role? :customer_support
can :read,:all
else
user.role? :marketing
can :read,:all
end
end
end
我在user.rb中添加方法
def role?(role)
self.roles.include? role.to_s
end
我也加
我的控制器中的load_and_authorize_resource说为products_controller,它可以授权用户并允许他在此控制器中执行某些操作,
但是我的问题是,当用户以管理员身份登录后,他无法添加新产品,它使cancan访问被拒绝。
我的看法是
<% if can? :create,Product %>
<td class=\"action\"><%= link_to \'Show\',product %></td>
<td class=\"action\"><%= link_to \'Edit\',edit_product_path( product) %></td>
<td class=\"action\"><%= link_to \'Destroy\',product,:confirm => \'Are you sure?\',:method => :delete %></td>
<% end %>
它也没有显示此管理员链接,因为对管理员有全部访问权限,但他仍然无法访问此操作?
我还想念什么?
请帮助?
解决方法
您是否遵循康康Wiki中的说明? https://github.com/ryanb/cancan/wiki/Role-Based-Authorization。
Cancan为每个用户存储角色的默认策略是使用位掩码,但是Wiki在此处提到了不同的解决方案:https://github.com/ryanb/cancan/wiki/Separate-Role-Model。