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

发布于 2024-10-12 15:27:59 字数 756 浏览 1 评论 0原文

鉴于我有一个嵌套在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自我难过 2024-10-19 15:27:59

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

load_resource :worker, :class => 'Worker', :through => :farm, :parent => false

Untested, but according to the docs/code, you should be able to specify the name as the first argument to load_resource:

load_resource :worker, :class => 'Worker', :through => :farm, :parent => false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文