Rails 4.2.7、Pundit、Mongoid 和 RailsAdmin 的未定义方法“to_criteria”
Undefined method `to_criteria' with Rails 4.2.7, Pundit, Mongoid and RailsAdmin
我有一个 Rails 4.2.7 应用程序,带有 rails_admin (0.8.1)、pundit (1.1.0) 和 mongoid (5.1.4)
我创建了 Campaign 脚手架并在 CampaignController 中将 authorize @campaign
添加到 set_campaign
。当我浏览到 http://localhost:3000/campaigns/57b34dd3f5740c23d3066e43 时,我得到 unable to find policy
CampaignPolicyfor <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
我"solved"通过像这样修改 CampaignPollicy 中的 Scope 但我想知道是否有更好的解决方案
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
好吧,想通了,我想我会分享答案。
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
我有一个 Rails 4.2.7 应用程序,带有 rails_admin (0.8.1)、pundit (1.1.0) 和 mongoid (5.1.4)
我创建了 Campaign 脚手架并在 CampaignController 中将 authorize @campaign
添加到 set_campaign
。当我浏览到 http://localhost:3000/campaigns/57b34dd3f5740c23d3066e43 时,我得到 unable to find policy
CampaignPolicyfor <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
我"solved"通过像这样修改 CampaignPollicy 中的 Scope 但我想知道是否有更好的解决方案
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
好吧,想通了,我想我会分享答案。
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