Skip to content
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

Closed
alanpaivaa opened this issue Jul 27, 2016 · 6 comments
Closed

Can not delete all images on a upload that handles multiple files #1990

alanpaivaa opened this issue Jul 27, 2016 · 6 comments

Comments

@alanpaivaa
Copy link

alanpaivaa commented Jul 27, 2016

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.

@arevutsky
Copy link

I have a same issue. I'm trying to solve it by myself

@dyerc
Copy link

dyerc commented Aug 26, 2016

I have this same issue, haven't had any success solving it yet

@jcarlos
Copy link

jcarlos commented Sep 6, 2016

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

@hanpeic
Copy link

hanpeic commented May 16, 2017

Thanks for Joao's help.

@alexey-klimuk
Copy link

I had the same issue. Did the following for setting empty array:
self[:images] = []

@ghost
Copy link

ghost commented Mar 20, 2018

I find out there is another way to do it with just params rails provided.

def destroy
        remove_image_at_index(params[:id].to_i)
        if @account.save
            flash[:success] = "delete successfull"
            redirect_to(account_path(@account))
        else
            flash.now[:danger] = "sorry can't delete the photo"
            render("show")
        end
end

In ImagesController.rb
@account is the model where mount_uploaders :images, ImagesUploader

private

def remove_image_at_index(index)
        remain_images = @account.images
            if index == 0 && @account.images.size == 1
                @account.remove_images!
            else
                deleted_image = remain_images.delete_at(index) 
                deleted_image.try(:remove!)
                @account.images = remain_images
            end
 end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants