【发布时间】:2014-01-31 00:47:59
【问题描述】:
我刚刚从 CanCan 切换到 Pundit。我不确定有几件事,以及如何最好地使用 Pundit。
例如:
如果您的资源可以有多个父对象,例如,假设一个目标属于学生和教师。因此,一个学生可以有很多目标,而一个教师可以有很多目标。在控制器索引操作中,您可能会这样做:
if params[:student_id].present?
@account = Student.find(params[:student_id])
@goals = @account.goals
elsif params[:instructor_id].present?
@account Instructor.find(params[:instructor_id])
@goals = @account.goals
end
params 在策略内部是不可用的,所以逻辑需要在这里完成。我认为。据我所知,如果您跳过policy_scope,您将在查看目标索引页面时收到未经授权的错误。
你会:
@goals = policy_scope(@account.goals)
或
@goals = policy_scope(Goal.scoped).where( account_id: @account.id)
当您在混合中添加一堆包含时会发生什么?
@example = policy_scoped(@school.courses.includes(:account => :user, :teacher ))
或者当需要订购时...这样正确吗?
policy_scope(Issue.scoped).order("created_at desc")
使用作用域时:这里的:scope 是什么? :scope 是被评估模型的实例吗?我尝试通过:scope 访问它的属性,但没有成功。
class Scope < Struct.new(:user, :scope)
【问题讨论】:
标签: ruby-on-rails-4 authorization pundit