简体   繁体   English

如何在 Rails Active Storage 中使用 DropzoneJS 进行多次上传?

[英]How do I do multiple uploads with DropzoneJS in Rails Active Storage?

I am following this tutorial to do multiple file uploads with DropzoneJS but I don't know how to get multiple images belonging to a model to show in the 'show' page.我正在按照本教程使用 DropzoneJS 进行多个文件上传,但我不知道如何将属于一个模型的多个图像显示在“显示”页面中。 Only one image shows when I upload multiple images, despite doing a .each in the @post.feature_image array.尽管在@post.feature_image数组中做了一个.each ,但当我上传多张图片时只显示一张图片。

Tutorial here: https://web.archive.org/web/20191223022642/https://web-crunch.com/rails-drag-drop-active-storage-stimulus-dropzone/教程在这里: https : //web.archive.org/web/20191223022642/https : //web-crunch.com/rails-drag-drop-active-storage-stimulus-dropzone/

Example Repo: https://github.com/justalever/drag_and_drop_active_storage示例回购: https : //github.com/justalever/drag_and_drop_active_storage

Changes I have made to get it working: _form.html.erb我为使其工作所做的更改:_form.html.erb

data-dropzone-max-files="10"

posts/show.html.erb帖子/show.html.erb

 <%= link_to @post do %>
    <% @post.feature_image.each do |image| %>
      <%= image_tag image %>
    <% end %>
  <% end %>

models/post.rb模型/post.rb

class Post < ApplicationRecord
  belongs_to :user
  has_many_attached :feature_image
end

Please help- what am I doing wrong?请帮助-我做错了什么?

Ran into the same issue with the same tutorial and goal, and I just figured it out!使用相同的教程和目标遇到了相同的问题,我刚刚想通了!

  1. Pluralize the name of your association, has_many_attached :feature_images将您的协会名称复数化, has_many_attached :feature_images
class Post < ApplicationRecord
  belongs_to :user
  has_many_attached :feature_images
end
  1. Add multiple: true to your file field (don't forget to also pluralize the association name there as well!)添加multiple: true到您的文件字段(不要忘记在那里也将关联名称复数化!)
<%= form.file_field :feature_images, direct_upload: true, multiple: true, data: { target: 'dropzone.input' } %>
  1. Update your post_params controller method to allow an array of feature_images rather than expecting one attachment:更新您的post_params控制器方法以允许一组feature_images而不是期望一个附件:
def post_params
  params.require(:post).permit(:title, :body, feature_images: [])
end

That should be all.这应该是全部。 Some more info here https://guides.rubyonrails.org/active_storage_overview.html#has-many-attached这里有更多信息https://guides.rubyonrails.org/active_storage_overview.html#has-many-attached

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

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