2016-10-26 4 views
0

Извините, я не видел другого места, чтобы задать вопрос о Пандит ... Спасибо за вашу помощь.Ruby - Rails 4 - Pundit - Ошибка политики и авторизации для маршрута #index_fr?

Я работаю над Ruby on rails API, и я хотел бы создать URL-адрес (.../api/v1/достопримечательности/fr), перечислить некоторую информацию об одной из моих моделей. Но я получил сообщение об ошибке от Пандита:

Пандита :: AuthorizationNotPerformedError в/API/v1/достопримечательность/FR Апи :: V1 :: AttractionsController

и эту ошибку для verify_authorized в файл Lib/pundit.rb

def verify_authorized 
    raise AuthorizationNotPerformedError, self.class unless pundit_policy_authorized? 
end 

Это моя конфигурация:

# app/config/routes.rb 

namespace :api, defaults: { format: :json } do 
    namespace :v1 do 
     resources :lines, only: [ :index, :show ] do 
     collection do 
      get '/fr', to: 'attractions#index_fr' 
     end 
     end 
    end 
end 

# app/controllers/api/v1/attractions_controller.rb 

class Api::V1::AttractionsController < Api::V1::BaseController 
skip_before_action :authenticate_user! 

    def index 
    @attractions = policy_scope(Attraction) 
    @attractions = Attraction.all 
    end 

    def index_fr 
    @attractions = policy_scope(Attraction) 
    @attractions = Attraction.all 
    end 
end 

# app/policies/application_policy.rb 

class ApplicationPolicy 
    attr_reader :user, :record 

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

    def index? 
    false 
    end 

    def index_fr? 
    false 
    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 
end 

ответ

0

Попробуйте добавить before_filter :skip_authorization в свой контроллер api.

Однако метод pundit verify_authorized следует вызывать, только если вы добавили его как after_action.

Смежные вопросы