简体   繁体   English

Rails活动存储has_many_attachments允许参数发布

[英]Rails active storage has_many_attachments permit params issue

I want to upload multiple images in form by using active storage but it gives an permit params issue Unpermitted parameter: :avatars 我想通过使用活动存储来上载表单中的多个图像,但是它给出了一个许可参数问题Unpermitted parameter: :avatars

params.require(:poi).permit(:title, :description,avatars: [])

In _form.html.erb , _form.html.erb

<%= form.file_field :avatars %>

In poi.rb , poi.rb

has_many_attached :avatars

In controller, 在控制器中

@poi = Poi.new(poi_params)

respond_to do |format|
  if @poi.save
    format.html { redirect_to @poi, notice: 'Poi was successfully created.' }
    format.json { render :show, status: :created, location: @poi }
  else
    format.html { render :new }
    format.json { render json: @poi.errors, status: :unprocessable_entity }
  end
end

How can I fix this issue? 如何解决此问题?

Refer here . 请参考这里 You have has_many_attached relation, Your code will work only for has_one_attached relation. 您具有has_many_attached关系,您的代码仅适用于has_one_attached关系。

For has_one_attached , 对于has_one_attached

<%= form.file_field :avatars %>

For has_many_attached , 对于has_many_attached

<%= form.file_field :avatars, multiple: true %>

For direct upload, 对于直接上传,

<%= form.file_field :attachments, multiple: true, direct_upload: true %>

Add multiple: true in your file field. 添加multiple: true在文件字段中为multiple: true The file_input should look like this: file_input应该如下所示:


<%= form.file_field :avatars, multiple: true %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM