我有一个模型“附件”配置为:
型号
class Attachment< ActiveRecord::Base
has_many_attached :files
end
COntroller
def remove
@attachment = Attachment.find_by_id(params[:attachment_id])
@upload = @attachment.files.find(params[:abc_id])
@upload.purge
redirect_back fallback_location: root_path, notice: 'Image was deleted successfully.'
end
查看文件:
<% attachment.files.each do |file| %>
<%= link_to image_tag(file , class: "resize__image", alt: "#{file.filename}"), show_image_attachment_path(file)%>
<div><%= file.filename %></div>
<div> <%= link_to "Remove", attachment_abc_remove_path( attachment, file), method: :delete, data: { confirm: "Are you sure?" }, remote: true %></div>
<% end %>
路线:
resources :attachments, only: [:show, :destroy] do
member do
get :show_image
end
resources :abc do
match '/remove', to: 'attachments#remove', via: 'delete'
end
end
当我尝试删除一个或多个图像时,“ active_storage_blobs”和“ active_storage_attachments”中的相应记录将被删除,而“ Attachment”表中的记录仍将存在。
使用活动存储是否可以预期?如果我也想从“帐户”表中删除相应的元素怎么办? 我尝试了许多方法,但无法从“帐户”表中删除记录。