7

I found useful article about uploading image using Active Storage in activeadmin: https://medium.com/@maris.cilitis/using-ruby-on-rails-active-storage-image-uploads-for-active-admin-backed-resources-5638a9ca0b46

But how to upload multiple images in activeadmin with Active Storage the same way?

Postanova
  • 97
  • 1
  • 6

2 Answers2

17

You just need do some changes

model:

has_many_attached :images

instead of

has_one_attached :image

activeadmin:

permit_params images: []

form do |f|
  f.inputs do
    f.input :images, as: :file, input_html: { multiple: true }
  end
end

and you'll can choose many files to upload

Alex
  • 186
  • 1
  • 6
  • it works after I removed this line `input_html: { multiple: true }` and added `form :html => { :multipart => true } do |f|` – Hany Moh. Jan 24 '20 at 15:35
  • 3
    The issue with this is that we don't have the option to keep the selected images on edit page. Each time we go to edit page we need to select all the images. Is there a way to pass the old selected images here – Nidhin S G Feb 18 '20 at 05:44
  • @NidhinSG you can borrow ideas from the solution posted by edariedl here: https://stackoverflow.com/questions/71990425/rails-active-storage-keep-existing-files-uploads – professormeowingtons Aug 20 '22 at 07:34
3

This works for me, upload and show multiple images in active_admin using active storage.

ActiveAdmin.register Post do

  permit_params :content, :published, :user_id, :images => []

  form html: { multipart: true } do |f|
    f.inputs "Publication" do
      f.input :user
      f.input :content
      f.input :published
      f.input :images, as: :file, input_html: { multiple: true }
    end

    f.actions
  end

  show do
    attributes_table do
      row :images do
        div do
          post.images.each do |img|
            div do
              image_tag url_for(img), size: "200x200"
            end
          end
        end
      end

      row :content
      row :published
    end
  end
end