Advertisement
Guest User

Untitled

a guest
Aug 8th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.68 KB | None | 0 0
  1. def create
  2.   add_more_images(images_params[:images])
  3.   flash[:error] = "Failed uploading images" unless @item.save
  4.   redirect_to admin_items_path
  5. end
  6.  
  7. def destroy
  8.   remove_image_at_index(params[:id].to_i)
  9.   flash[:error] = "Failed deleting image" unless @item.save
  10.   redirect_to admin_items_path
  11. end
  12.  
  13. def add_more_images(new_images)
  14.   images = @item.images
  15.   images += new_images
  16.   @item.images = images
  17. end
  18.  
  19. def remove_image_at_index(index)
  20.   remain_images = @item.images # copy the array
  21.   deleted_image = remain_images.delete_at(index) # delete the target image
  22.   deleted_image.try(:remove!) # delete image from S3
  23.   @item.images = remain_images # re-assign back
  24. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement