Rails 5.2 Active Storage不会删除附加的图像

时间:2019-06-19 08:32:43

标签: ruby-on-rails rails-activestorage

我正在尝试删除附在工作职位上的图像。但是,单击“删除”后,附加的图像仍然存在,并且不会从工作岗位中删除。

工作展示页面

<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

1 个答案:

答案 0 :(得分:1)

在我的控制器中,我将'npm:': '/node_modules/'替换为:

@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