当嵌套模型的名称与控制器名称不同时,如何使用 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>
请注意,如果我在返回 Workers 集合的 Farm 模型中定义了 farm_workers() getter,那么我不会收到错误 - 尽管 Workers 集合未加载索引操作。无论如何,我不想污染我的模型以使控制器身份验证正常工作。
(这应该不重要,但我正在使用 mongoid)
given that I have a Worker model which is nested under Farm model, how do I correctly load the Worker resource in the Workers controller (called FarmWorkersController)? I've tried this...
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
... but I get the error
undefined method `farm_workers' for #<Farm:0xa87670c>
Note that if I define a farm_workers() getter in the Farm model which returns the collection of Workers, then I don't get the error - though the Workers collection is not loaded for the index action. In any case, I don't want to pollute my model to make the controller authentication work.
(It shouldn't matter but I am using mongoid)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
未经测试,但根据文档/代码,您应该能够将名称指定为
load_resource
的第一个参数:Untested, but according to the docs/code, you should be able to specify the name as the first argument to
load_resource
: