Ruby on rails 可以授权关联对象
My Routes.rb:Ruby on rails 可以授权关联对象,ruby-on-rails,associations,cancan,Ruby On Rails,Associations,Cancan,My Routes.rb: resources :users resources :clients do resources :branches end 我有这个能力。用户只能更新自己的客户端 can [:update], Client, :id => user.clients.pluck(:id) 用户还应该能够:创建、:更新、:显示属于客户端的分支。它可能看起来像这样: can [:create, :update, :show], Branch, :client =&g
resources :users
resources :clients do
resources :branches
end
我有这个能力。用户只能更新自己的客户端
can [:update], Client, :id => user.clients.pluck(:id)
用户还应该能够:创建、:更新、:显示属于客户端的分支。它可能看起来像这样:
can [:create, :update, :show], Branch, :client => { :id => user.clients.pluck(:id) }
它适用于:更新、:显示,但不适用于:创建。这是因为新分支在创建之前没有客户端id。
如何使其适用于:create?您当前的能力实际上没有对分支上的外键字段进行检查。像这样使用嵌套就是调用等价的
client.id
,而不是像您希望的那样调用client\u id
应改为:
can[:创建、:更新、:显示]、分支、客户端\u id:user.client\u id
或者,与您的风格保持一致:
can[:create,:update,:show],Branch,client\u id:user.clients.pluck(:id)