问题描述
我添加了一个控制器collaborators
来管理Users
和Companies
之间的特定类型的联接关联.问题是,每当我从collaborators
加载任何内容时,都会出现错误
I've added a controller collaborators
to manage a particular type of join association between Users
and Companies
. The issue is that whenever I load anything from collaborators
, I get the error
据我了解,这是因为没有模型Collaborator
,并且我正在使用cancancan
进行授权.从旧的cancan(请注意,不是cancancan)文档,我已经能够收集到没有相应模型的控制器需要为其手动授权的模型,例如:load_and_authorize_resource :the_model, :parent => false
.
From my understanding, this is because there is no model Collaborator
and I am using cancancan
for authorization. From the old cancan (note not cancancan) documentation, I've been able to gather that controllers that don't have a corresponding model need to have a model manually authorized for them something like: load_and_authorize_resource :the_model, :parent => false
.
如果我在application.rb控制器中禁用load_and_authorize_resource
,这似乎可以正常工作.
This seems to work if I disable load_and_authorize_resource
in my application.rb controller.
我的问题是:用cancancan授权没有相应模型的控制器的最佳方法是什么?我可以在应用程序控制器中继续load_and_authorize_resource
吗?
SO my quesestion is: what is the best way to authorize controllers that don't have corresponding models with cancancan? Can I continue to load_and_authorize_resource
in my application controller?
非常感谢.
推荐答案
此 LINK 会有所帮助.
在链接中,我引用
class ToolsController < ApplicationController
authorize_resource :class => false
def show
# automatically calls authorize!(:show, :tool)
end
end
在您的ability.rb
中:
class Ability
include CanCan::Ability
def initialize(user)
can :show, :tool
end
end
这篇关于使用cancancan时添加没有相应模型的Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!