当嵌套模型的名称与控制器名称不同时,如何使用cancan加载嵌套资源?

时间:2011-01-19 13:53:44

标签: ruby-on-rails authorization cancan

鉴于我有一个嵌套在Farm模型下的Worker模型,如何在Workers控制器(称为FarmWorkersController)中正确加载Worker资源?我试过这个......

class FarmWorkersController < ApplicationController
  load_resource :farm, :parent => true
  load_resource :class => 'Worker', :through => :farm, :parent => false

  # Note that :parent and :class need to be specified on the Worker resource line,
  # as the name of the controller (FarmWorkersController) is different from Worker model name
end

...但我收到了错误

undefined method `farm_workers' for #<Farm:0xa87670c>

请注意,如果我在Farm模型中定义了farm_workers()getter,它返回了Workers集合,那么我不会收到错误 - 尽管未为index集合加载Workers集合。无论如何,我不想污染我的模型以使控制器认证工作。

(应该没关系,但我使用的是mongoid)

1 个答案:

答案 0 :(得分:3)

未经测试,但根据文档/代码,您应该能够将名称指定为load_resource的第一个参数:

load_resource :worker, :class => 'Worker', :through => :farm, :parent => false
相关问题