TrestleAdmin / trestle

A modern, responsive admin framework for Ruby on Rails
https://trestle.io
GNU Lesser General Public License v3.0
1.94k stars 173 forks source link

Multiple File upload? #272

Open samnymr opened 4 years ago

samnymr commented 4 years ago

Hiya,

I've been looking for multiple file upload, I'm currently using the file_field option.

Is it possible to have a multiple file upload system? I don't know if I'm missing something haha!

spohlenz commented 4 years ago

The file_field helper works the same as the corresponding Rails form helper (https://api.rubyonrails.org/v6.0.2.1/classes/ActionView/Helpers/FormHelper.html#method-i-file_field) so you can add support for multiple file uploads with:

file_field :attachments, multiple: true

Your models will also need to be hooked up correctly with something like ActiveStorage or Shrine.

coezbek commented 2 years ago

Documented here: https://github.com/TrestleAdmin/trestle/wiki/Form-Fields#file-field

Timmitry commented 2 years ago

The file_field :attachments, multiple: true works so far. However, when editing an entity that already has attachments, this might destroy all old attachments and replace them with the new ones. That is due to built-in behaviour of Rails, which was changed from 5.2 to 6.0. There are different ways to keep the old attachments:

  1. Add hidden fields with the existing attachments (related Issue)

    record.attachments.each do |attachment|
      hidden_field :attachments, multiple: true, value: attachment.signed_id
    end
    file_field :attachments, multiple: true
  2. Set rails config option config.active_storage.replace_on_assign_to_many = false - however, this changes the behaviour for the whole application! (Related Issue)