2016-08-17 59 views
0

我有一個Rails 4.2.7應用程序與rails_admin(0.8.1),pundit(1.1.0)和mongoid(5.1.4)未定義的方法`to_criteria'與Rails 4.2.7,Pundit,Mongoid和RailsAdmin

我創建了Campaign腳手架並在CampaignController中添加了authorize @campaignset_campaign。當我瀏覽到http://localhost:3000/campaigns/57b34dd3f5740c23d3066e43時我得到unable to find policy CampaignPolicy 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 
相關問題