如何使用 pundit permitted_attributes 允许多个文件

How to allow file multiple with pundit permitted_attributes

我有一个问题模型和多态附件关联。 Pundit 用于处理授权和强参数。当我使用简单的文件输入作为附件时,像这样:

<%= f.simple_fields_for :attachments do |attachments| %>
  <%= attachments.input :file, as: :file %>

一切顺利,但当我为文件输入添加多个时,专家拒绝了此属性。这是 policy

允许的属性方法
def permitted_attributes
  [
      :title, :description, :project,
      assigned_users_attributes: [:_destroy, :id, :email],
      attachments_attributes: [:file]
  ]
end

我应该如何更改它以接受多个文件输入?

您需要将 :file 更改为 :file => [] 以允许多个值。

def permitted_attributes
  [
    :title, :description, :project,
    assigned_users_attributes: [:_destroy, :id, :email],
    attachments_attributes: [:file => []]
  ]
end