3

controller show action

def show
   @batch = Batch.find(params[:id])
   @batch_id = @batch.id
   authorize @batch
end

pundit policy

def show?
puts @batch_id
    if !current_user.nil? && (current_user.role?('Student')) ||  (current_user.role?('Administrator')) || (current_user.role?('SupportSpecialist')) || (current_user.role?('Instructor'))
      true
    else
      false
    end
  end

I am getting nil when I puts @batch_id, how can I get that value in policy action

Praveen Gowda I V
  • 9,569
  • 4
  • 41
  • 49
Gopi Raju
  • 219
  • 3
  • 13

1 Answers1

4

Your controller:

def show
  @batch = Batch.find(params[:id])
  @batch_id = @batch.id
  authorize @batch
end

Your policy file might be:

class BatchPolicy
  attr_reader :user, :record
 
  def initialize(user, record)
    @user = user
    @record = record
  end
     
  def show?
    true if record ...  // record will be equal @batch
  end  
end

If you want additional context, then you must know:

Pundit strongly encourages you to model your application in such a way that the only context you need for authorization is a user object and a domain model that you want to check authorization for. If you find yourself needing more context than that, consider whether you are authorizing the right domain model, maybe another domain model (or a wrapper around multiple domain models) can provide the context you need.

Pundit does not allow you to pass additional arguments to policies for precisely this reason.

There you can learn about it from the horse's mouth.

Community
  • 1
  • 1
atomdev
  • 321
  • 1
  • 8