Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails_admin and pundit: undefined method `policy' for #<RailsAdmin::MainController

enter image description here I'm on rails 5 and I'm trying to implement authorizations with pundit for my rails_admin panel. So I included pundit in my application controller and installed the rails_admin_pundit gem as you can see in this snippet of my Gemfile:

gem 'devise'
gem 'devise-i18n'
gem 'rails_admin', '~> 1.0'
gem 'rails_admin-i18n'
gem 'rails_admin_tag_list', github: 'kryzhovnik/rails_admin_tag_list'
gem 'pundit'
gem "rails_admin_pundit", :github => "sudosu/rails_admin_pundit"

The application policy:

class ApplicationPolicy
  attr_reader :current_user, :record

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

  def index?
    false
  end

  def show?
    scope.where(:id => record.id).exists?
  end

  def create?
    false
  end

  def new?
    create?
  end

  def update?
    false
  end

  def edit?
    update?
  end

  def destroy?
    false
  end

    def rails_admin?(action)
        case action
        when :dashboard
            @user.admin?
        when :index
            @user.admin?
        when :show
            @user.admin?
        when :new
            @user.admin?
        when :edit
            @user.admin?
        when :destroy
            @user.admin?
        when :export
            @user.admin?
        when :history
            @user.admin?
        when :show_in_app
            @user.admin?
        else
            raise ::Pundit::NotDefinedError, "unable to find policy #{action} for #{record}."
        end
    end

end

And a snippet of my rails_admin initializer:

RailsAdmin.config do |config|
  config.authorize_with :pundit
  config.current_user_method(&:current_user)
  ...
end

So now when I load the admin dashboard (url: "/admin") I get this error:

undefined method `policy' for #RailsAdmin::MainController:0x0055914e2523a0>

I followed all the instructions, but I still don't see what's missing. Any answer/suggestion will be greatly appreciated.

like image 386
Badr Tazi Avatar asked Nov 02 '16 10:11

Badr Tazi


People also ask

How to install pundit in rails?

Within your application controller include Pundit. Run command bundle install. Optionally run rails g pundit:install which will set up an application policy with some useful defaults. The Policies will be defined in app/policies/ directory. And don’t forget to restart the Rails server so that Rails can pick up new classes that you define there.

Where can I find the policies in rails?

The Policies will be defined in app/policies/ directory. And don’t forget to restart the Rails server so that Rails can pick up new classes that you define there.

What is the use of pundit in Ruby?

This is where you can make leverage of Pundit. Pundit helps us to define policies which are PORC - Plain Old Ruby Classes - which means that the class does not inherit from other classes nor include in other modules from the framework. Thus makes it very easy to understand the code. We would still need to define roles for our Users.

What is pundit and how does it work?

And that is Pundit. So what is Pundit? When there arises need for restricting access to your application for certain users, role based authorization comes into play. This is where you can make leverage of Pundit.


1 Answers

This bugged me too. Eventually found: https://travis-ci.org/sferik/rails_admin/jobs/152180750

rails_admin now defaults to ::ActionController::Base

To fix, put the following in rails_admin.rb:

config.parent_controller = '::ApplicationController'

like image 192
Jason Avatar answered Sep 29 '22 00:09

Jason