使用cancancan时添加没有相应模型的Controller [英] Adding a Controller without corresponding model while using cancancan

查看:117
本文介绍了使用cancancan时添加没有相应模型的Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了一个控制器collaborators来管理UsersCompanies之间的特定类型的联接关联.问题是,每当我从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

未初始化的常量协作者

uninitialized constant Collaborator

据我了解,这是因为没有模型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 cancancanfor 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆