0
votes

I'm using CarrierWave and Cloudinary to upload multiple pictures to my blogposts. I upload them from my browser. This happens with the use of a file field in the post form.

<%=file_field_tag "images[]", type: :file, multiple: true %>

When the form is being submitted, a picture instance is created for each one of the images and the image is being uploaded to Cloudinary.

def create
    @post = Post.new(post_params)
    if @post.save
        if params[:images]
            params[:images].each do |image|
                @post.pictures.create(image: image)
                Cloudinary::Uploader.upload(image)
            end
        end
    end
end

The ImageUploader I use is almost default (exept for including the cloudinary plugin)

class ImageUploader < CarrierWave::Uploader::Base
  include Cloudinary::CarrierWave
  storage :file
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end 
end

Now, the image is being saved to my server, and the image is being uploaded to cloudinary. But somehow the public_id's never match. Does anyone understand why not? Is there a new public_id created when I call Cloudinary::Uploader.upload(image)?

2

2 Answers

2
votes

After checking the project that Tal Lev-Ami (thanks a lot for this!) refered me to, I figured out that the problem with my ImageUploader was the line

storage :file

After commenting out this line, everything worked perfectly (and I don't have to manually upload my images). If anyone understands why this line was causing the problem, be my guest to post it here for future reference.

1
votes

There is no need to manually upload the image to Cloudinary. When using the CarrierWave uploader, the image will get automatically uploaded to Cloudinary and public_id updated in the model.