无法清除活动存储附件

Can't purge active storage attachments

我有一个名为 Resource 的模型,配置为:

class Resource < ActiveRecord::Base    
has_many_attached :assets
end

我在 resources_controller.rb 中创建了一个动作,如下所示:

  def delete_asset_attachment
    @asset = ActiveStorage::Attachment.find_by(params[:id])
    logger.debug "The value of @asset is #{@asset}"
    @asset.purge
    redirect_to @resource
  end

我有一个显示资源并循环访问附加资产的表单。下面是通过资产进行循环的代码片段:

<% @resource.assets.each do |asset| %>
<%= link_to 'Remove Attachment', delete_asset_attachment_resource_url(@resource, asset.id), method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>

/resources 页面正确显示了资源以及附加的资产。但是,当我尝试单击 link 删除其中一项资产时,我收到错误消息:"undefined method `purge' for nil:NilClass"。但是,在控制台中我看到附件存在。

这是服务器控制台的输出:

    Started DELETE "/resources/10/delete_asset_attachment.18" for ::1 at 2019-03-09 17:27:28 -0500
Processing by ResourcesController#delete_asset_attachment as 
  Parameters: {"authenticity_token"=>"EFZO5V9Bii3dId0I6hn5DajFR5WJYZBc8qPAAi5ppQOFW3cws5I4FjyVP9IlvA+2a2kKUJhobnqd8atG4L3k+g==", "id"=>"10"}
  Resource Load (0.1ms)  SELECT  "resources".* FROM "resources" WHERE "resources"."id" = ? LIMIT ?  [["id", 10], ["LIMIT", 1]]
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ActiveStorage::Attachment Load (0.1ms)  SELECT  "active_storage_attachments".* FROM "active_storage_attachments" WHERE (10) LIMIT ?  [["LIMIT", 1]]
The value of @asset is #<ActiveStorage::Attachment:0x00007f8d7bd4df68>
  ActiveStorage::Blob Load (0.2ms)  SELECT  "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = ? LIMIT ?  [["id", 27], ["LIMIT", 1]]
Completed 500 Internal Server Error in 5ms (ActiveRecord: 0.6ms)

NoMethodError - undefined method `purge' for nil:NilClass:

::1 - - [09/Mar/2019:17:27:28 EST] "POST /resources/10/delete_asset_attachment.18 HTTP/1.1" 500 76939
http://localhost:3000/resources/10/edit -> /resources/10/delete_asset_attachment.18
Started POST "/__better_errors/70da0e976a425fce/variables" for ::1 at 2019-03-09 17:27:28 -0500
  ActiveStorage::Blob Load (0.2ms)  SELECT  "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = ? LIMIT ?  [["id", 27], ["LIMIT", 11]]
::1 - - [09/Mar/2019:17:27:28 EST] "POST /__better_errors/70da0e976a425fce/variables HTTP/1.1" 200 36499
http://localhost:3000/resources/10/delete_asset_attachment.18 -> /__better_errors/70da0e976a425fce/variables

我到处寻找解决方案。 Whosebug 上存在的那对夫妇没有解决我的问题。 Rails 指南或网络上的其他任何地方都极度缺乏专门处理删除附件的具体细节和示例。非常感谢任何帮助。

更新:这是我的 routes.rb:

资源:资源做 得到 'listing', :on => :collection 放 :sort, on: :collection 会员做 删除:delete_asset_attachment 结尾 结束

更新 2:rails 路由输出

resources GET    /resources(.:format)                                                                     resources#index
                             POST   /resources(.:format)                                                                     resources#create
                new_resource GET    /resources/new(.:format)                                                                 resources#new
               edit_resource GET    /resources/:id/edit(.:format)                                                            resources#edit
                    resource GET    /resources/:id(.:format)                                                                 resources#show
                             PATCH  /resources/:id(.:format)                                                                 resources#update
                             PUT    /resources/:id(.:format)                                                                 resources#update
                             DELETE /resources/:id(.:format)                                                                 resources#destroy

我已经能够完成这项工作。啊哈。经过数小时的挫折之后,匆忙。

读完这篇文章后把事情拼凑起来 从控制器中删除 ActiveStorage 附件,3 种方法

我将控制器代码更改为:

  def delete_asset_attachment
    @resource.assets.find_by(params[:attachment_id]).purge
    redirect_to @resource
  end

我的表格是这样的:

   <% @resource.assets.each do |asset| %>
      <%= asset.filename %>
      <%= link_to 'Remove Attachment', delete_asset_attachment_resource_url(@resource, asset.id), method: :delete, data: { confirm: 'Are you sure?' } %>
   <% end %>

我认为问题出在我旧代码中的行:

@asset = ActiveStorage::Attachment.find_by(params[:id])

...仅传递@resource id,未找到附件。关键是改变这一行:

@resource.assets.find_by(params[:attachment_id]).purge

...更恰当地指向正确的资源,然后指向要清除的特定资产(附件)。

修复 find_by 的语法并使用安全符号

    @asset = ActiveStorage::Attachment.find_by(params[:id])
    @asset.purge

尝试:

    @asset = ActiveStorage::Attachment.find_by(id: params[:id])
    @asset&.purge