Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Active Admin to work with Pundit after login

I've added the configuration pundit addapter authorization to my application

config.authorization_adapter = ActiveAdmin::PunditAdapter

When I login with the [email protected] credentials I'm getting this error.

Pundit::NotDefinedError in Admin::Dashboard#index
unable to find policy AdminUserPolicy

Extracted source (around line #2):

insert_tag active_admin_application.view_factory["page"]

so I created these files in my policies/active_admin folder

adminuser_policy.rb

module ActiveAdmin
class AdminUserPolicy < ApplicationPolicy
class Scope < Struct.new(:user, :scope)
  def resolve
    scope
  end
end
def home?
true
end

def index?
true 
end
def show?
true 
end
def new?
true
end

def create?
 true
end

def update?
true 
end

  def destroy?
    true 
 end
end

end

page_policy.rb

module ActiveAdmin
class PagePolicy < ApplicationPolicy
  class Scope < Struct.new(:user, :scope)
  def resolve
    scope
  end
 end
   def index?
      true
   end

   def show?
     true
   end
  end
end

What am I missing? Thanks for the help!

like image 473
user3787971 Avatar asked Jan 11 '15 02:01

user3787971


People also ask

How does active admin work?

Active Admin is a Ruby on Rails plugin for generating administration style interfaces. It abstracts common business application patterns to make it simple for developers to implement beautiful and elegant interfaces with very little effort.

Why we use pundit gem?

The pundit gem is a policy based authorization system, which makes everything easier to implement. We can create different policies for different models and permit or restrict any user action based on the types of user roles.


1 Answers

I found the answer!

After adding these two lines to the active admin initializer file

config.authorization_adapter = ActiveAdmin::PunditAdapter 

#this line sets the default policy to application_policy.rb
config.pundit_default_policy = "ApplicationPolicy"

I had to add this to dashboard.rb under app/admin/dashboard.rb

def index
  authorize :dashboards, :index?
end

Then I created a file in my policies folder called dashboard_policy.rb and added this code

class DashboardPolicy < ApplicationPolicy
   def dashboard?
   true
  end
  def index?
   true
  end
 end

That got it working!

like image 149
user3787971 Avatar answered Oct 20 '22 18:10

user3787971