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

Question: Is there no way to remove a singe image with Multiple file uploads? #1704

Closed
lcx opened this issue Jul 22, 2015 · 17 comments
Closed

Comments

@lcx
Copy link

lcx commented Jul 22, 2015

I'm using the master branch with the Multiple file upload feature, postgresql and have a model "products" with

   t.string  "images", array: true

I can store multiple images just fine, I can remove images with remove_images! but how can I remove just a single image?
Tried SO with no luck :(
http://stackoverflow.com/questions/31381121/carrierwave-multiple-image-upload

I looked at the mount_multiple_spec.rb and as far as I can see there is no way to remove a single image.
Please correct me if I'm wrong.

@cloudy9101
Copy link

Me too. Have you solve it?

@nacengineer
Copy link
Contributor

AFAIK its all or nothing... same thing with refile unfortunately. the joys of file uploading via the web

@jorgeandresserrano
Copy link

Hi guys, any advance on this one?

@popup-user
Copy link

Yes, this is important for me, as well, thanks! - and I'm using jsonb as my datatype in postgresql..

@lcx
Copy link
Author

lcx commented Dec 22, 2015

I did answer my own question on SO: http://stackoverflow.com/questions/31381121/carrierwave-multiple-image-upload

@bobintornado
Copy link

I learn from @lcx and use the following solution at my own project.

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

I wrote a blog post on this with a sample project.

I hope this is helpful for other people.

@rehan-dev
Copy link

@bobintornado this delete another photo like i have 4 photos when i am going to delete 2 photo its delete 3 photo and this delete function work sometime not every time kindly need a proper solution.

@bobintornado
Copy link

Is the bug reproducible?
On Mar 16, 2016 14:09, "Rehan Khan" notifications@github.com wrote:

@bobintornado https://github.com/bobintornado this delete another photo
like i have 4 photos when i am going to delete 2 photo its delete 3 photo
and this delete function work sometime not every time kindly need a proper
solution.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#1704 (comment)

@anduong
Copy link

anduong commented Nov 8, 2016

@bobintornado Hi Bob, I have used your solution (https://github.com/carrierwaveuploader/carrierwave/wiki/How-to%3A-Add-more-files-and-remove-single-file-when-using-default-multiple-file-uploads-feature) in my system. And I think there is a performance issue with it.

For example, my Gallery model has 7 images, then I remove 1 one of them. I guess the code removes 1 image then reuploads and processes again 6 images. That takes a lot of time.

It does that because I think when we re-assign back the new array of images to our Gallery model, it will treat that new array of 6 images as 6 new images not 6 old ones. Therefore, it does upload and process everything.

Do you guys have any solution to add and remove one image in the array without reuploading images?

@anduong
Copy link

anduong commented Nov 8, 2016

@bobintornado
Copy link

bobintornado commented Nov 8, 2016

@anduong Yes there are quite some performance issues regarding this approach.

How PostgreSQL handles array is one the reason I am aware of.

This is by no means a sophisticated method of handling multiple images, and I suppose maybe you should consider writing some customised handling codes for this if performance becomes a concern. 😄

@anduong
Copy link

anduong commented Nov 8, 2016

@bobintornado Thanks for your suggestion. I think the way CarrierWave handles multiple uploads with array type in database is so inflexible. Maybe extracting out another model (say Images) and using one-many or many-to-many relationship is a better way to go.

By the way, how is your system performing? Is it slow when adding or removing images?

@bobintornado
Copy link

@anduong I agree. Actually the first idea comes to me is the extraction of another model, too.

Unfortunately I no longer monitor and responsible for the maintenance of the project where I developed this method for, so I am not sure about the performance situation.

Another approach is stopping do it the rails way, separate the frontend and backend. For example, write a JS script for handling multiple images related UI and interactions, and handle the input in the backend however you'd like to. This way you have a much greater flexibility. 🤓

@anduong
Copy link

anduong commented Nov 8, 2016

@bobintornado Couldn't agree with you more. My last project was not doing the Rails way (just using S3 direct upload) and it was much more flexible.

@russellrichardson
Copy link

I went for something more like this:

  @object.images[index].try(:remove!) # delete image from S3
  @object['images'].delete_at(index) # remove from images array
  @object.save
  @object.reload # if you need to reference the new set of 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

@mshibuya
Copy link
Member

Closed by #2401.

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

No branches or pull requests