Search code examples
ruby-on-railsscopeactiveadminpolicypundit

Rails Pundit ActiveAdmin: page isn’t redirecting properly


I installed Activeadmin and Pundit gems.

Added 'include Pundit' in application_controller.rb.

Defined package_policy.rb

class PackagePolicy < ApplicationPolicy
  def update?
    user.admin?
  end
end

application_policy.rb:

class ApplicationPolicy
  attr_reader :user, :record

  def initialize(user, record)
    @user = user
    @record = record
  end

  def index?
    false
  end

  def show?
    false
  end

  def create?
    false
  end

  def new?
    create?
  end

  def update?
    false
  end

  def edit?
    update?
  end

  def destroy?
    false
  end

  def scope
    Pundit.policy_scope!(user, record.class)
  end

  class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user = user
      @scope = scope
    end

    def resolve
      scope
    end
  end
end

And than I get

page isn’t redirecting properly Firefox has detected that the server is redirecting the request for this address in a way that will never complete

in my browser. Maybe, it is infinity loop or something like it.

I had some different configures of package_policy.rb, but after added application_policy.rb - the result is always error in browser after trying to log in to Activeadmin panel.


Solution

  • I allowed all actions for all methods in my ApplicationPolicy.

    And after I created new policies with needed permissions for my resources.

    In ApplicationPolicy:

    ...
      def index?
        true
      end
    
      def show?
        true
      end
    
      def create?
        true
      end
    
      def new?
        create?
      end
    
      def update?
        true
      end
    
      def edit?
        update?
      end
    
      def destroy?
        true
      end
    ...
    

    In any other policy, for example:

    ...
      def index?
        user.admin?
      end
    
      def show?
        user.admin?
      end
    
      def create?
        user.admin?
      end
    
      def new?
        create?
      end
    
      def update?
        user.admin?
      end
    
      def edit?
        update?
      end
    
      def destroy?
        user.admin?
      end
    ...