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

Removing single image when using multiple file uploads feature, doesn't work when only one image left #2024

Closed
bobbybobby opened this issue Sep 21, 2016 · 6 comments

Comments

@bobbybobby
Copy link

According to the wiki https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Add-more-files-and-remove-single-file-when-using-default-multiple-file-uploads-feature it's possible to remove only one image of a column where mount_uploaders is used with the following manipulation :

remain_images = @gallery.images # copy the array
deleted_image = remain_images.delete_at(index) # delete the target image
deleted_image.try(:remove!) # delete image from S3
@gallery.images = remain_images # re-assign back

However I found that in case there is only one image left in the array it doesn't work. I can workaround that by checking the length of the array and calling @gallery.remove_images! in that case, but that would be great for code consistency if it always worked.

@anduong
Copy link

anduong commented Nov 8, 2016

I have another issue with this code (commented here #1704 (comment))

@bobintornado
Copy link

@bobbybobby Hi bobby I am aware of the issue but haven't dived into solving it. As I don't really have a clue why this happens yet. 😕

@anduong
Copy link

anduong commented Nov 8, 2016

@bobbybobby @bobintornado I have this issue too and others have confirmed it. There is a workaround for it. See #1990

@bobintornado
Copy link

@anduong thanks I see! 😃

@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

@mshibuya
Copy link
Member

mshibuya commented May 1, 2019

Merging into #1990.

@mshibuya mshibuya closed this as completed May 1, 2019
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

4 participants