0

I am getting this error when i am trying to authorize through pundit and authenticate through Devise.

ArgumentError - wrong number of arguments (1 for 0):
      pundit (1.1.0) lib/pundit.rb:194:in `authorize'
      app/controllers/trips_controller.rb:23:in `new'
      actionpack (4.2.5) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
      actionpack (4.2.5) lib/abstract_controller/base.rb:198:in `process_action'

My application_controller.rb is

class ApplicationController < ActionController::Base

  include Pundit

  protect_from_forgery with: :exception

  rescue_from Pundit::NotAuthorizedError, with: :permission_denied
   ....
end

Have a application_policy.rb as follows

class ApplicationPolicy
  attr_reader :user, :record

  def initialize(user, record)
    @user = 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 scope
    Pundit.policy_scope!(user, record.class)
  end

  class Scope
    attr_reader :user, :scope

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

    def resolve
      scope
    end
  end
end

My trip_policy.rb

class TripPolicy < ApplicationPolicy
    def new?
    user.provider?
  end

  def create?
    user.provider?
  end

    def update?
    user.provider?
  end

end

And in my trips_controller i am doing this

class TripsController < ApplicationController
    before_action :authenticate_user!
  after_action :verify_authorized, except: [:index, :new]

  ...
  def new
    @trip             = Trip.new
    authorize @trip
  end

  ...

end

Any pointer why am i getting this error. Have been breaking my head for some time now.

Raj Singh
  • 11
  • 3
  • if i use `raise "not authorized" unless TripPolicy.new(current_user, @trip).new?` it works. if i use `Pundit.authorize current_user, @trip, "new?"` it works. but some how its getting confused when i just use the method directly. – Raj Singh Apr 15 '16 at 05:35

1 Answers1

0

Figured out the issue. It was a problem with my code. Inside the authorize function it refers to Policy function which accepts a parameter.

In my application_controller.rb i had another policy function which did not accept any parameters, which was causing the issue. Renaming my policy function to something else solved the problem.

Sometimes it pays to sleepover the problem and wake up the next day with a fresh start.

Raj Singh
  • 11
  • 3