Adding a Controller without corresponding model while using cancancan

旧巷老猫 提交于 2019-12-07 21:19:53

问题


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

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.

This seems to work if I disable load_and_authorize_resource in my application.rb controller.

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?

Many thanks in advance.


回答1:


This LINK will help.

From the link, I quote,

class ToolsController < ApplicationController
  authorize_resource :class => false
  def show
    # automatically calls authorize!(:show, :tool)
  end
end

And in your ability.rb:

class Ability
  include CanCan::Ability

  def initialize(user)
    can :show, :tool
  end
end


来源:https://stackoverflow.com/questions/34763269/adding-a-controller-without-corresponding-model-while-using-cancancan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!