CanCan之间的区别:read和?

根据所有文档, :read操作都是别名:index:show

 alias_action :index, show, :to => :read 

但是,请考虑使用嵌套资源的以下方案:

 resources :posts resources :comments end 

如果我定义这样的能力:

 # ability.rb can :read, Post can :show, Comment # comments_controller.rb load_and_authorize_resource :organization, :find_by => :permalink load_and_authorize_resource :membership, :through => :organization 

事情按预期工作。 但是,如果我将:read操作更改为[:index,:show]:

 # ability.rb can [:index, :show], Post can :show, Comment # comments_controller.rb load_and_authorize_resource :organization, :find_by => :permalink load_and_authorize_resource :membership, :through => :organization 

我未经授权访问/posts/:post_id/comments/posts/:post_id/comments/:id等。但是,我仍然可以访问:index:show for posts_controller

如果这些行为的行为不同,这些行为有可能是“别名”吗?

在我的摆弄中,我也遇到了以下情况。 将load_and_authorize_resource更改为以下允许的访问权限:

 # ability.rb can [:index, :show], Post can :show, Comment # comments_controller.rb load__resource :organization, :find_by => :permalink load_and_authorize_resource :membership, :through => :organization 

有人能解释一下这里发生了什么吗?

我在GitHub上发布了这个问题。 Ryan回答如下:

:index:show actions都指向:read动作。 但是当CanCan授权父资源时,它直接使用:read动作,这就是你看到这种行为的原因。

我认为这之前引起了混淆,所以我会改变内部行为,永远不要直接使用:read动作。 而不是:parent资源我将其更改为使用:show和对于accessible_by默认我将使用:index而不是:read 。 谢谢你引起我的注意。

https://github.com/ryanb/cancan/issues/302#comment_863142