CanCan之间的区别:read和[:index,:show]?

set*_*rgo 10 ruby-on-rails cancan ruby-on-rails-3

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

alias_action :index, show, :to => :read
Run Code Online (Sandbox Code Playgroud)

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

resources :posts
  resources :comments
end
Run Code Online (Sandbox Code Playgroud)

如果我定义这样的能力:

# 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
Run Code Online (Sandbox Code Playgroud)

事情按预期工作.但是,如果我将: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
Run Code Online (Sandbox Code Playgroud)

我是未经授权的访问/posts/:post_id/comments,/posts/:post_id/comments/:id等我还在,但是,可以同时访问:index:showposts_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
Run Code Online (Sandbox Code Playgroud)

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

set*_*rgo 16

我在GitHub上发布了这个问题.瑞安回应如下:

无论是:index:show行动指向:read作用.但是当CanCan授权父资源时,它会:read直接使用该操作,这就是您看到此行为的原因.

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

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