title: Index View Restrictions for Various Roles using Pundit
body: <p>I'm trying to create a show view for three roles. Admin, super user, and user. An admin should see all of the users. A super user should see only users and a user should not see anyone. When I used the commented out policy method in the resolve for <code>else user.super_user?</code> would give me <code>unsupported: TrueClass</code> error. Any suggestions are welcomed.</p>
  
  <p><em>Users Controller</em></p>
  
  <pre><code>def index
    @users = policy_scope(User)
    authorize User
  end
  </code></pre>
  
  <p>User Policy</p>
  
  <pre><code>class UserPolicy
   attr_reader :current_user, :model
  
   def initialize(current_user, model)
     @current_user = current_user
     @user = model
   end
  
   class Scope
     attr_reader :user, :scope
  
     def initialize(user, scope)
        @user = user
        @scope = scope
     end
  
     def resolve
       if user.admin?
         scope.all
       else user.super_user?
         scope.where(user.role = 'user' )
         # scope.where(user.role != 'admin') [this would not work in the views, but it would work in rails c]
         end
       end
     end
  
    def index?
      @current_user.admin? or @current_user.super_user?
    end
  end
  </code></pre>
  
  <p><strong>updated</strong> <em>Users Controller</em></p>
  
  <pre><code>class UsersController &lt; ApplicationController
    before_filter :authenticate_user!
    after_action :verify_authorized
  
    def index
      @users = policy_scope(User)
    end
  end
  </code></pre>
  
  <hr>
  
  <p><strong>Correct Answer</strong></p>
  
  <p>I figured out what I needed to do. I was calling the role incorrectly. Updated scope below.</p>
  
  <pre><code>class UserPolicy
    attr_reader :current_user, :model
  
    def initialize(current_user, model)
      @current_user = current_user
      @user = model
    end
  
    class Scope
      attr_reader :user, :scope
  
      def initialize(user, scope)
         @user = user
         @scope = scope
      end
  
      def resolve
        if user.admin?
          scope.all
        else user.super_user?
          scope.where(role: 'user' )
        end
      end
    end
  
    def index?
      @current_user.admin? or @current_user.super_user?
    end
  </code></pre>
  
  <p><em>controller</em></p>
  
  <pre><code>class UsersController &lt; ApplicationController
    before_filter :authenticate_user!
    after_action :verify_authorized
  
    def index
      @users = policy_scope(User)
      authorize @users
    end
  </code></pre>