ruby-on-railsruby-on-rails-4model-view-controllercancancancancan

Adding a Controller without corresponding model while using cancancan


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.


Solution

  • 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