New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can not delete all images on a upload that handles multiple files #1990
Comments
I have a same issue. I'm trying to solve it by myself |
I have this same issue, haven't had any success solving it yet |
I had the same problem and seems to be a bug. When we assign an empty array to the method images= it doesn't seem to update the internal attribute images of the model. My code is a bit different from what you are using because I am deleting files based on the filename and not the index but the important bit is the lines if self.images.empty? && read_attribute(:images).size == 1 # when the method images is empty already but the internal attribute still has an element
write_attribute(:images, []) # remove the element from the internal attribute
end This line basically overwrites the internal images attribute of my model so it become empty when the image method is empty. This when when the object is saved the internal images attributes is updated to match the method images. The full function I am using inside my model is below: def delete_image(image_file)
image_to_delete = self.images.select do |image| # find the image based on file name
image.file.filename == image_file
end
if image_to_delete && image_to_delete[0] # delete the image that was found
self.images = self.images - [image_to_delete[0]]
if self.images.empty? && read_attribute(:images).size == 1 # update the internal attribute
write_attribute(:images, [])
end
image_to_delete[0].remove! # delete from the file system
save! # save the updated images attribute
end
end If I have time later I will look inside carrierwaveuploader sourcecode and try to find which line is making things out of sync so we can have a proper fix instead of the workaround I wrote |
Thanks for Joao's help. |
I had the same issue. Did the following for setting empty array: |
I find out there is another way to do it with just params rails provided.
In ImagesController.rb
|
Hi,
I'm using the master branch for handling multiple file uploads. It's working fine when adding new images, but I can not delete all of the images.
My model:
class MyModel < ActiveRecord::Base
mount_uploaders :images, MyModelImagesUploader
end
My controller:
images = @my_model.images
image = images.delete_at(params[:image_index].to_i)
image.remove!
@my_model.images = images
@my_model.save
When I delete all images, it says that the last image is still there, although the file itself is not there.
The text was updated successfully, but these errors were encountered: