我的Routes.rb:
resources :users
resources :clients do
resources :branches
end我在Ability.rb上有这个。用户只能更新自己的客户端。
can [:update], Client, :id => user.clients.pluck(:id)用户还应该能够:创建、:update、:显示属于客户端的分支。它可能看起来像这样:
can [:create, :update, :show], Branch, :client => { :id => user.clients.pluck(:id) }它适用于:update,:show,但不适用于:create。这是因为新分支在创建之前没有client_id。我如何使它发挥作用:创造?
发布于 2015-03-27 17:12:15
您当前的能力实际上不是对分支上的外键字段调用检查。像这样使用嵌套是在调用等效的client.id,而不是您想要的client_id。
应改为:
can [:create, :update, :show], Branch, client_id: user.client_ids
或者,保持与你的风格一致:
can [:create, :update, :show], Branch, client_id: user.clients.pluck(:id)
https://stackoverflow.com/questions/28624916
复制相似问题