本文介绍了使用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

未初始化的常量协作者

据我所知,这是因为没有模型Collaborator,我使用cancan进行授权.从旧的cancan(注意不是cancancan)文档,我已经能够收集到没有相应模型的控制器需要为它们手动授权模型,例如:load_and_authorize_resource :the_model, :parent =>错误.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:00