Rails 5 CarrierWave,无法删除多个上传中的最后一个文件
Rails 5 CarrierWave, can't remove last file in a multiple upload
遵循此操作方法:
class ImagesController < ApplicationController
before_action :set_gallery
def create
add_more_images(images_params[:images])
flash[:error] = "Failed uploading images" unless @gallery.save
redirect_to :back
end
def destroy
remove_image_at_index(params[:id].to_i)
flash[:error] = "Failed deleting image" unless @gallery.save
redirect_to :back
end
private
def set_gallery
@gallery = Gallery.find(params[:gallery_id])
end
def add_more_images(new_images)
images = @gallery.images
images += new_images
@gallery.images = images
end
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
@gallery.images = remain_images # re-assign back
end
def images_params
params.require(:gallery).permit({images: []}) # allow nested params as array
end
end
我似乎无法正确删除最后一个文件。在我打印的文件列表中,它一直站在那里。奇怪的是 0kb。
然后当我加载新文件时,这个文件就会消失。
我遇到了同样的问题,我发现如果这是最后一个,您必须调用 'remove_images!'。在 'remove_image_at_index' 函数中添加:
@gallery.remove_images! if remain_images.empty?
此致
遵循此操作方法:
class ImagesController < ApplicationController
before_action :set_gallery
def create
add_more_images(images_params[:images])
flash[:error] = "Failed uploading images" unless @gallery.save
redirect_to :back
end
def destroy
remove_image_at_index(params[:id].to_i)
flash[:error] = "Failed deleting image" unless @gallery.save
redirect_to :back
end
private
def set_gallery
@gallery = Gallery.find(params[:gallery_id])
end
def add_more_images(new_images)
images = @gallery.images
images += new_images
@gallery.images = images
end
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
@gallery.images = remain_images # re-assign back
end
def images_params
params.require(:gallery).permit({images: []}) # allow nested params as array
end
end
我似乎无法正确删除最后一个文件。在我打印的文件列表中,它一直站在那里。奇怪的是 0kb。
然后当我加载新文件时,这个文件就会消失。
我遇到了同样的问题,我发现如果这是最后一个,您必须调用 'remove_images!'。在 'remove_image_at_index' 函数中添加:
@gallery.remove_images! if remain_images.empty?
此致