【问题标题】:How do I do multiple uploads with DropzoneJS in Rails Active Storage?如何在 Rails Active Storage 中使用 Dropzone JS 进行多次上传?
【发布时间】:2020-03-01 14:58:55
【问题描述】:

我正在按照本教程使用 DropzoneJS 进行多个文件上传,但我不知道如何获取属于模型的多个图像以显示在“显示”页面中。尽管在@post.feature_image 数组中做了.each,但我上传多张图片时只显示一张图片。

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

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

我为使其正常工作所做的更改: _form.html.erb

data-dropzone-max-files="10"

posts/show.html.erb

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

models/post.rb

class Post < ApplicationRecord
  belongs_to :user
  has_many_attached :feature_image
end

请帮忙——我做错了什么?

【问题讨论】:

    标签: javascript ruby-on-rails ruby ruby-on-rails-5


    【解决方案1】:

    使用相同的教程和目标遇到了相同的问题,我只是想通了!

    1. 复数您的协会名称,has_many_attached :feature_images
    class Post < ApplicationRecord
      belongs_to :user
      has_many_attached :feature_images
    end
    
    1. multiple: true 添加到您的文件字段(不要忘记在此处也将关联名称复数!)
    <%= form.file_field :feature_images, direct_upload: true, multiple: true, data: { target: 'dropzone.input' } %>
    
    1. 更新您的post_params 控制器方法以允许feature_images 的数组,而不是期望一个附件:
    def post_params
      params.require(:post).permit(:title, :body, feature_images: [])
    end
    

    应该就是这样。更多信息在这里https://guides.rubyonrails.org/active_storage_overview.html#has-many-attached

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-16
      • 2018-11-14
      • 2015-09-10
      • 2019-11-19
      • 1970-01-01
      • 2020-02-01
      • 1970-01-01
      • 2020-04-30
      相关资源
      最近更新 更多