Pundit :: NotAuthorizedError / Проблема с авторизацией pundit - PullRequest
1 голос
/ 05 августа 2020

Я пытаюсь обновить адрес пользователя в форме, но я не понимаю, почему я не авторизован для выполнения, это мой код:

class AddressesController < ApplicationController
  def update
    @address = current_user.addresses.last
    authorize @address
    @address.update!(address_params)
  end

  private

  def address_params
    params.require(:address).permit(:first_name, :last_name, :city, :country, :postcode, :phone_number, :street_address, :optional_address, :user_id)
  end
end


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

    def update?
      true
    end
  end
end

, и это ошибка:

Pundit :: NotAuthorizedError в AddressesController # update не разрешено обновлять? этот адрес

1 Ответ

1 голос
/ 05 августа 2020

Вы определили метод update? внутри вложенного класса Scope, но он должен быть определен непосредственно в классе политики.

Вместо этого:

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

    def update?
      true
    end
  end
end

Для этого нужно:

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

  def update?
    true
  end
end
...