我正在按照本教程使用 DropzoneJS 进行多个文件上传,但我不知道如何获取属于模型的多个图像以显示在“显示”页面中。尽管在 @post.feature_image
数组中执行了 .each
,但当我上传多张图像时,仅显示一张图像。
示例仓库: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 %>
模型/post.rb
class Post < ApplicationRecord
belongs_to :user
has_many_attached :feature_image
end
请帮忙 - 我做错了什么?
最佳答案
在相同的教程和目标下遇到了相同的问题,我刚刚解决了!
- 您的关联名称采用复数形式,
has_many_attached :feature_images
class Post < ApplicationRecord
belongs_to :user
has_many_attached :feature_images
end
- 将
multiple: true
添加到您的文件字段(不要忘记同时将关联名称复数化!)
<%= form.file_field :feature_images, direct_upload: true, multiple: true, data: { target: 'dropzone.input' } %>
- 更新您的
post_params
Controller 方法以允许一组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
关于javascript - 如何在 Rails Active Storage 中使用 Dropzone JS 进行多次上传?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60476387/