1

I have created custom method in rails 4

def duplicate    
    new_house = @house.amoeba_dup  
    respond_to do |format|
        if new_house.save        
           format.html { render action: 'new', notice: 'Category Attribute Added Successfully' }
        else
           format.html { render action: 'new' }        
        end
    end    
end

But it give Pundit::AuthorizationNotPerformedError when I call duplicate method.

tirdadc
  • 4,603
  • 3
  • 38
  • 45

1 Answers1

1

This is happening because Pundit is detecting that your new controller method is not checking for authorization. This typically is triggered by a line like this in your controller:

after_action :verify_authorized

So change your new method to this:

def duplicate    
  new_house = @house.amoeba_dup
  authorize new_house
  respond_to do |format|
    if new_house.save        
      format.html { render action: 'new', notice: 'Category Attribute Added Successfully' }
    else
      format.html { render action: 'new' }        
    end
  end    
end

You also need to update your house_policy.rb to add a duplicate? method. The example below assumes the permission is the same as for the create method:

# policies/house_policy.rb
class HousePolicy < ApplicationPolicy
  def duplicate?
    create?
  end
tirdadc
  • 4,603
  • 3
  • 38
  • 45