1
votes

I'm using Cloudinary and CarrierWave to handle my image uploads. This is the guide that I'm following. I have a Collection model with a cover column of type: string:

collection.rb

class Collection < ActiveRecord::Base
  has_many :posts
  belongs_to :user
  mount_uploader :cover, ImageUploader
end 

And below is my image_uploader.rb:

class ImageUploader < CarrierWave::Uploader::Base

  include Cloudinary::CarrierWave
  include CarrierWave::MiniMagick

  version :default do
    cloudinary_transformation quality: "auto", width:640, crop: "scale", fetch_format: "auto"
  end

end

I'm using simple_form for the upload form:

= simple_form_for @collection do |f|
  = f.input :cover, label: false, include_blank: true, input_html: { onChange: 'window.loadFile(event)' }

Now, the uploading works fine. For every image uploaded, the cover column of that particular Collection changes to the URL of the uploaded image. No issues there. BUT, I can't seem to find a way to delete an uploaded image. Sure, I can replace an uploaded image with another, but I can't remove it, so that the cover column becomes nil again. I've tried updating the attribute to nil or '' from both the upload form and the rails console, but neither works. I've also tried @collection.remove_cover! as mentioned in the CarrierWave guide, which didn't work either. Can someone help?

1

1 Answers

1
votes

You can call the delete method on the file attribute of your Carrierwave object, e.g.:

@collection.cover.file.delete

Assuming the delete_remote methods is set to true (default), it will remove the instance from your model, as well as from Cloudinary storage.

See: https://github.com/cloudinary/cloudinary_gem/blob/master/lib/cloudinary/carrier_wave.rb#L172