2
votes

I'm using Cloudinary and CarrierWave to upload images from my Rails application, and it works fine.

My requirement is that a user can have a one image, so if a user already have an image and if he/she uploads a new one the previous image should be overridden by the new one.

My problem is, when I upload the new image to Cloudinary it is not invalidating the previous image and hence old image is still shown as users image.

Then I found an option called invalidate and tried to use it, but no luck.

This is my Cloudinary class

class PictureUploader < CarrierWave::Uploader::Base
  include Cloudinary::CarrierWave

  version :show do
    process :invalidate => true 
  end
end

and it my view

recipe.picture_url(:show)

but this shows the old image and not the new one. What am I missing?

1

1 Answers

4
votes

When a Cloudinary image is accessed for the first time, it gets cached in the CDN. You can indeed update the image, by re-uploading while keeping the public ID, but if you access the same URL, you might be still delivered with the CDN cached version of the image.

You can tell Cloudinary to invalidate the image through the CDN, however note that enabling the invalidate parameter should be included at the upload process, and not inside the 'versions' block, as invalidation is applied upon re-uploading and not on delivery. Also note that it might take up to an hour for the invalidation to fully propagate through the CDN.

It is recommended to use the versions component instead. Adding the 'versions' component to the URL tells Cloudinary to force delivery of the latest version of the image while bypassing CDN cached versions. The updated version value is returned with every upload call. For more information: http://cloudinary.com/documentation/rails_image_manipulation#image_versions

While it takes a while for invalidation to propagate, the 'version' component affects immediately.