trestle icon indicating copy to clipboard operation
trestle copied to clipboard

Multiple File upload?

Open samnymr opened this issue 5 years ago • 3 comments
trafficstars

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!

samnymr avatar Dec 27 '19 13:12 samnymr

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.

spohlenz avatar Jan 01 '20 04:01 spohlenz

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

coezbek avatar Dec 14 '21 07:12 coezbek

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)

Timmitry avatar Jan 19 '22 11:01 Timmitry