【问题标题】:Rails 5.2 Active Storage not deleting attached imagesRails 5.2 Active Storage 不删除附加图像
【发布时间】:2019-11-01 22:29:46
【问题描述】:

我正在尝试删除附加到职位发布的图片。但是点击删除后,附加的图像仍然存在,并且不会从职位发布中删除。

职位展示页面

<div class="ui text container slides">
        <i class=" left angle icon"></i>
        <i class=" right angle icon"></i>
       <% @job.images.each_with_index do |image, index| %>
        <div class="slide active">
          <%= image_tag image, size: 200 %> 
 <%= link_to 'Remove', delete_image_attachment_job_url(image.signed_id),
                    method: :delete,
                    data: { confirm: 'Are you sure?' } %>
                </div> 
         <% end %>
    </div>
  </div>

路线

  resources :jobs do
  member do
    delete :delete_image_attachment
  end
end

jobs_controller.rb

def delete_image_attachment
    @image = ActiveStorage::Blob.find_signed(params[:id])
    @image.purge
    redirect_to root_path
end

【问题讨论】:

  • 请将控制器跟踪添加到问题中。
  • 提交删除操作时,您在应用程序控制台中看到了什么?
  • @cnnr 没有特别提到任何东西,只是表明 Active_Storage 处理了图像并且没有提到它被删除

标签: ruby-on-rails rails-activestorage


【解决方案1】:

在我的控制器中,我将@image = ActiveStorage::Blob.find_signed(params[:id]) 替换为:

def delete_image_attachment
    @image = ActiveStorage::Attachment.find(params[:id])
    @image.purge
    redirect_back(fallback_location: job_path) 
end

我现在的删除操作是:

<%= link_to 'Remove', delete_image_attachment_job_url(image),
                method: :delete,
                data: { confirm: 'Are you sure?' } %>

【讨论】:

    猜你喜欢
    • 2018-10-24
    • 2019-01-01
    • 1970-01-01
    • 2020-09-03
    • 2018-09-05
    • 2023-04-09
    • 2020-11-04
    • 2018-09-28
    • 1970-01-01
    相关资源
    最近更新 更多