当资源名称与模型/控制器名称不同时,Cancan的load_and_authorize_resource如何处理?

3
在我的应用程序中,我有一个sheets控制器,模型名称为Sheet,但我的路由如下:
routes.rb
 namespace :magazine do
  resources :pages, :controller => "sheets" do
    resources :articles do
     resources :comments

这样url就会变成magazine/page/1/article...

在我的文章控制器中,如何调用load_and_authorize_resource来加载“sheets”,以便我可以访问相关工作表的文章。 我尝试过

load_and_authorize_resource :sheet, :class => 'Sheet', :parent => false
load_and_authorize_resource :through => :sheet 

无法访问 @sheet.articles......

1个回答

8

你可能有以下情况:

 load_and_authorize_resource :page, :class => 'Sheet', :parent => false

您可以使用@pages来访问您的数据。

或者您可以替换为:

 load_and_authorize_resource :sheet, :class => 'Sheet', :parent => false

你可以使用@sheets访问你的数据。


在ArticlesController中,要获取sheetarticles,请使用以下代码:

load_and_authorize_resource :sheet, :class => 'Sheet'
load_and_authorize_resource :article, :through => :sheet 

依旧是这个问题...load_and_authorize_resource :sheet, :class => 'Sheet', :parent => false load_and_authorize_resource :through => :sheet 无效。 - Oatmeal
为什么需要使用 parent - apneadiving
你在哪个控制器里?你需要什么数据? - apneadiving
我正在Article控制器中,因为我的文章与sheet相关联,所以我首先加载sheet,然后使用:article,:through => :sheet。在Article创建操作中 => @article = @sheet.articles.build(params[:article]) ---- 不起作用。 - Oatmeal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接