首页 > 解决方案 > 如何编写 CanCanCan 用户只能读取其数据的能力?

问题描述

您如何限制用户访问以使用户只能读取自己的记录?

我试过了:

def initialize(user)
  can :read, User, :id => user.id

和这个:

def initialize(user)
  can :read, user

但我仍然可以访问索引和显示中的每个用户。我在 UsersController 中有 authorize_resource。

相关文档供参考: https ://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities

标签: ruby-on-railsauthorizationcancancancancan

解决方案


:read != :show

:read == [:show, :index]

不幸的是,我没有设置来测试它,所以它是在黑暗中拍摄的。

can :show, User, :id => user.id

推荐阅读