0

Context: I am creating a bookmark feature for my app and it seems to be running and then it hits the Pundit error Pundit::AuthorizationNotPerformedError in SavedHairstylesController#create

It's very strange as I have a) generated a policy for my bookmark model called saved_hairstyle_policy.rb, in this policy I have returned "true" for each method.

b) In my Saved_Hairstyles controller I have a CREATE and DESTROY method and within each method I have written authorize @saved_hairstyle

c) In my view I have referred to the policy like so : policy(Saved_Hairstyle).create?

Any ideas?

Code to follow:

Saved_hairstyle_policy.rb:

class SavedHairstylePolicy < ApplicationPolicy
  class Scope < Scope
    def resolve
      scope.all
    end
  end

  def create?
    return true
  end

  def destroy?
    return true
  end
end

Saved_Hairstyle Controller:

class SavedHairstylesController < ApplicationController
   def create    
    @hairstyle = Hairstyle.find(params[:hairstyle_id])
    authorize @saved_hairstyle
  (current user) &  hairstyle
    @saved_hairstyle = SavedHairstyle.new(user: current_user, hairstyle: @hairstyle)  
    if @saved_hairstyle.save
      respond_to do |format|   
        format.html { redirect_to hairstyle_path(@saved_hairstyle.hairstyle) }
        format.js 
      end
    else
      respond_to do |format|
        format.html { render 'hairstyles' }
        format.js  
      end
    end
  end

  def destroy    
    @saved_hairstyle = SavedHairstyle.find(params[:id])
    authorize @saved_hairstyle    
    @saved_hairstyle.destroy
    @hairstyle = @saved_hairstyle.hairstyle
    respond_to do |format|
      format.html { redirect_to hairstyle_path(@saved_hairstyle.hairstyle)}
      format.js
    end
  end
end

index.html view file:

 <div class="bookmark">
              <% saved_hairstyle = SavedHairstyle.find_by(user: current_user, hairstyle: hairstyle.id) %>
              <% if saved_hairstyle && policy(Saved_Hairstyle).create?  %>
                <%= link_to saved_hairstyle_path(saved_hairstyle), method: :delete do %>
                  <i class="fas fa-plus-circle"></i>
                <% end %>
              <% elsif %>
                <% !saved_hairstyle && policy(Saved_Hairstyle).delete?  %>
                <%= link_to hairstyle_saved_hairstyles_path(hairstyle), method: :post do %>
                  <i class="fas fa-plus"></i>
                <% end %>
              <% end %>
            </div>

Angela Inniss
  • 359
  • 1
  • 2
  • 18
  • Take a look at this https://stackoverflow.com/questions/35071428/punditauthorizationnotperformederror – SgtPepper May 02 '19 at 16:27
  • Yes I checked that thread but as you can see I have called ''authorize @saved_hairstyle'' in my methods. I will try skipping the authorisation as suggested in that other thread – Angela Inniss May 02 '19 at 20:58

0 Answers0