我正在使用主分支的多个文件上传,我的产品型号有一个名为“PostgreSQL”的字符串字段,我可以附加多个图像。
但我不明白的是,我如何从产品中删除一个图像?我可以删除文档中描述的所有图像:
product.remove_images!
product.save但没有找到删除单个图像的方法。
发布于 2015-07-18 11:00:39
您是否考虑过使用的嵌套表单来尝试删除图像?
这是carrierwave的github站点上的一段代码...
<%= form_for @product, html: { multipart: true } do |f| %>
.
.
.
<label>
<%= f.check_box :remove_images %>
Remove images
</label>
<% end %>...which我相信你已经看过了。当然,调用remove_images!在您的情况下是行不通的,因为这意味着对所有映像执行统一的操作。
尝试对上述内容进行以下修改,看看它是否能帮助您对此问题进行排序,并分别针对每个图像……
<%= nested_form_for @product, :html=>{ :multipart => true } do |f| %>
.
.
.
<%= f.fields_for :images do |product_form| %>
<%= product_form.link_to_remove "Remove this image" %>
<% end %>
<% end %>要做到这一点,请确保在Gemfile中包含gem 'nested_form', '~> 0.3.2'。
希望这能帮到你。
发布于 2015-12-30 20:01:19
我从@Cris学习,并在使用S3时使用以下代码片段。
def remove_image_at_index(index)
remain_images = @product.images # copy the array
deleted_image = remain_images.delete_at(index) # delete the target image
deleted_image.try(:remove!) # delete image from S3
@product.images = remain_images # re-assign back
end我为此写了一个blog post,并用更具体的代码创建了一个sample project。
发布于 2015-07-13 19:13:05
您应该能够对指定的映像(从阵列)使用remove_image。因此,您需要先以某种方式识别图像(例如,images[0].remove_image!)
https://stackoverflow.com/questions/31381121
复制相似问题