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

Cannot remove the last image file of a collection! #2141

Closed
alphamarket opened this issue Mar 17, 2017 · 4 comments
Closed

Cannot remove the last image file of a collection! #2141

alphamarket opened this issue Mar 17, 2017 · 4 comments

Comments

@alphamarket
Copy link

alphamarket commented Mar 17, 2017

I want to selectively remove the uploaded images as a solution provided by @bobintornado at #1704.
Everything works fine except that when I want to delete the last image in my collection It does not assign the images value to [] the below is the debugging mode that I tested the procedure manually and deliberately assigned the images to [] but the model persists in maintaining its previous value!!

It removes the file from file system but comes short in blanking it in database

I guess you have to open the below image in new tab and zoom in to see details :D

image

The code is as following:
image

The database schema:

create_table "furniture_types", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
  t.string   "name"
  t.text     "comment",         limit: 65535
  t.datetime "created_at",                                    null: false
  t.datetime "updated_at",                                    null: false
  t.json     "images"
  t.boolean  "is_inside_type",                default: false
  t.boolean  "is_outside_type",               default: false
end

How to fix this?

@alphamarket
Copy link
Author

I found the source of the issue, it remains at the /lib/carrierwave/orm/activerecord.rb#L66

if !(new_file.blank? && send(:#{column}).blank?)

It refuses the blank file! I guess @eavgerinos didn't think the case that maybe someone wants to remove the images at #1571 when he commit the line!

@OleksandrPoltavets
Copy link

You can little bit modify solution from here and go with something like this:

  def remove_image_at_index(index)
    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
    if remain_images.any?
      @gallery.images = remain_images # re-assign back
    else
      @gallery.remove_images! # use regular file removal method
    end
  end

I will remove your file if only one left in array.

@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

3 participants