2016-08-17 11 views
0

rails_admin (0.8.1), 평범한 사람 (1.1.0), 몽고이 (5.1.4)Rails 4.2.7, Pundit, Mongoid and RailsAdmin과 정의되지 않은 메소드`to_criteria '

이있는 Rails 4.2.7 앱이 있습니다.

CampaignController에서 캠페인 스카 폴드를 만들고 authorize @campaignset_campaign에 추가했습니다. 내가 http://localhost:3000/campaigns/57b34dd3f5740c23d3066e43을 방문했을 때 unable to find policy 캠페인 정책 for <Campaign _id ...

rails g pundit:policy campaign을 실행하여 CampaignPolicy를 만들고 지금 작업을 표시합니다. 하지만 RailsAdmin http://localhost:3000/admin/campaign를 탐색 할 때 내가 얻을 :

undefined method `to_criteria' for Campaign:Class 
lib/mongoid/criteria.rb merge! method 

내가 같이 CampaignPollicy 내부의 범위를 수정하여 "해결"하지만 더 나은 솔루션을

class CampaignPolicy < ApplicationPolicy 
    def show? 
    true 
    end 
    class Scope 
    attr_reader :user, :scope 
     def initialize(user, scope) 
     @user = user 
     @scope = scope 
    end 
    def resolve 
    end 
    end 
end 

답변

0

OK가 있는지 궁금하고, 그것을 생각 나는 대답을 나누겠다고 생각했다.

class CampaignPolicy < ApplicationPolicy 
    ... 
    class Scope 
    def resolve 
     if @user.admin? 
     scope.all # this prevents undefined method `to_criteria' 
     else 
     scope.where(...) # put some biz logic here 
     end 
    end 
    end 
end