授权专家中的记录协会

Authorize record associations in pundit

在 rails 中与专家一起创建和更新记录时我们如何授权关联。

例如,如果我们正在更新属于 postcomment 记录,我们需要确保用户有权访问 post,否则它是安全漏洞。

create 操作中,您可以执行以下操作:

def create
  authorize @comment.post # this checks the authorization of Post
  authorize @comment # and then for comment
  #.. then code to create the comment
end