public_id doesn't match between cloudinary and carrierwave

若如初见. 提交于 2019-12-12 03:18:17

问题


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)?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/32493427/public-id-doesnt-match-between-cloudinary-and-carrierwave

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!