【发布时间】:2020-04-20 14:16:30
【问题描述】:
在我的编辑页面上,我显示了上传到活动存储数据库的产品的所有图像,并在其下方为该单个图像提供了一个专用的删除按钮。如果我手动插入 id,代码会删除图像,但我的代码发送的 id 给出了图像在数组中的位置。如何从数组位置编号中找到链接到图像的实际 id?
如果有更简单的方法可以做到这一点,也将不胜感激。
views/admin/products/_formedit.html.erb
<% (0...@admin_product.images.count).each do |image| %>
<%= image_tag(@admin_product.images[image]) %>
<%= link_to 'Remove', delete_image_attachment_admin_product_url(image), method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
controllers/admin/products_controller.rb
def delete_image_attachment
@image = ActiveStorage::Attachment.find(params[:id])
@image.purge
redirect_to contact_url
end
routes.rb
namespace :admin do
resources :products do
member do
delete :delete_image_attachment
end
end
end
【问题讨论】:
-
这是图像对象吗? @admin_product.images[图片]
标签: ruby-on-rails activerecord ruby-on-rails-5 rails-activestorage