2

I don't use user only admin_user

I tried to do as written here, but it doesn't work for me.

ApplicationController:

class ApplicationController < ActionController::Base
  include Pundit

  def pundit_user
    current_admin_user
  end
end

AdminUserPolicy:

  def destroy?
    pundit_user.id != admin_user.id
  end

undefined local variable or method `pundit_user' for #<AdminUserPolicy:0x00007fda70fd4df8>

How can i pass current_admin_user to pundit?

UPD. ApllicationPolicy.rb

class ApplicationPolicy
  attr_reader :user, :record

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

  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

  class Scope
    attr_reader :user, :scope

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

    def resolve
      scope.all
    end
  end
end
SsPay
  • 177
  • 1
  • 10
  • Can you post your base `ApplicationPolicy` class as well? – eugen Oct 29 '20 at 15:55
  • @eugen of course, i updated question – SsPay Oct 29 '20 at 16:20
  • 1
    I can't say anything for certain since you've only shown us a little snippet of the `AdminUserPolicy`, rather than the whole thing. But *probably* the way you should be defining that `destroy?` method is: `user.id != record.id` (where `user` is the pundit user, i.e. the `current_admin_user`; and the `record` is the entity you're trying to authorize the action against, i.e. an `admin_user`) – Tom Lord Oct 29 '20 at 17:54
  • 2
    *Generally speaking*, every pundit policy should only reference two things: `user` and `record`. – Tom Lord Oct 29 '20 at 17:56
  • 2
    You don't need to reference `user` by `pundit_user` on your policy. Use `user` in there like you'd use regularly. – cesartalves Oct 29 '20 at 19:55
  • 1
    @SsPay just like what cesartalves pointed out the user refers to your current_admin_user object – theterminalguy Oct 30 '20 at 03:15

0 Answers0