【发布时间】:2017-06-26 05:54:22
【问题描述】:
我指的是我自己的问题Rails Nested Resources with Pundit Allowing Index 并最终提出了一个可行的解决方案,但是在 property_policy 中定义 scope.where(?) 或 scope.select(?) 没有更好的解决方案吗?如何使用权威解析方法获取仅属于一个特定交易的所有属性?
我最终做了什么:
properties_controller.rb
class PropertiesController < ApplicationController
before_action :set_deal, except: [:index, :all]
before_action :set_property, only: [:show, :edit, :update, :destroy]
def all
@properties = Property.all
authorize @properties
end
def index
@deal = Deal.find(params[:deal_id])
@properties = policy_scope(Deal)
end
def set_deal
@deal = Deal.find(params[:deal_id])
# pundit ######
authorize @deal
###############
end
(...)
end
property_policy.rb
class PropertyPolicy < ApplicationPolicy
class Scope < Scope
def resolve
scope.all if user.admin?
end
def all?
user_is_admin?
end
def user_is_admin?
user.try(:admin?)
end
(...)
end
我想要更好的:
properties_controller.rb
def index
@deal = Deal.find(params[:deal_id])
@properties = policy_scope(Property) # => for # @properties = @deal.properties
authorize @deal
end
在 property_policy.rb 中类似
def resolve
# scope.where(???) if user.admin? # only an admin user can see the @deal.properties
# or any other solution using scope
end
提醒一下,1 个交易有许多属性,1 个属性属于一个特定交易。我的路线是嵌套交易/id/properties,除了我有简单的“/properties”的完整属性列表。非常感谢您的帮助。
** 更新 **
我终于去了
properties_controller.rb
def index
@deal = Deal.find(params[:deal_id])
@properties = policy_scope(@deal.properties)
authorize @properties, :index?
end
在property_policy.rb中
class PropertyPolicy < ApplicationPolicy
class Scope < Scope
def resolve
user.admin? ? scope.all : scope.none
end
end
def index?
user_is_admin?
end
def user_is_admin?
user.try(:admin?)
end
end
不确定是否正确
【问题讨论】:
标签: ruby-on-rails scope nested pundit