Carrierwave upload and save to database only one file

Viewed 250

Carrierwave only saves the first image all the time. It works after the "update" action, but shows an error:

undefined method `reject' for "image/upload/

It stores a string with a simple only one file path in a jsonb field in the db. But why does this work so and how do I fix it?

Code in model:

mount_uploaders :images, ImageUploader

Code of an uploader:

class ImageUploader < CarrierWave::Uploader::Base
  include Cloudinary::CarrierWave
  CarrierWave.configure do |config|
    config.cache_storage = :file
  end
  def extension_allowlist
    %w(jpg jpeg gif png)
  end
end

Code of a view:

    = f.input :images, label: t("photos"), as: :file, input_html: { multiple: true, class: "form-control"}

    - @post.images.each do |image|
      = hidden_field :post, :images, multiple: true, value: image.identifier

    - if @post.images.any?
      .form-group
        %div.form-check
          = f.check_box :remove_images, label: t("posts.remove_images"), class: "form-check-input", id: "removeImages"
          %label.form-check-label{for: "removeImages"}
            = t("posts.remove_images", count: @post.images.count)
1 Answers

This happens because Cloudinary::CarrierWave don't know how to work with carrierwave multiple file uploads feature(https://github.com/carrierwaveuploader/carrierwave#multiple-file-uploads) - it is incompatible. Cloudinary::CarrierWave - put string in field images "img1" and save just one image, but carrierwave expects array of images ['img1', 'img2'], and this raise undefined method `reject' for 'str...'

I recommend you implement own version of multi images upload if you want to use it with cloudinary. Or contribute in cloudinary gem :D

PS: i checked it locally

Related