我有一个名为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页面正确显示资源以及附加资产。但是,当我尝试单击链接以删除其中一项资产时,收到错误消息:“未定义的方法'purge'为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
我到处都在寻找解决方案。存在于stackoverflow上的夫妇并没有解决我的问题。在Rails指南或Web上的其他任何地方都缺少用于处理删除附件的特定细节和示例,这令人难以置信。不胜感激。
更新:这是我的routes.rb:
resources:resources做 获得“列表”,: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
答案 0 :(得分:0)
我已经能够完成这项工作。啊经过数小时的挫折之后,仓促。
阅读本文后将事情拼凑在一起 Deleting ActiveStorage Attachments From the Controller, 3 Ways
我将控制器代码更改为:
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
...更正确地指向正确的资源,然后指向要清除的特定资产(附件)。
答案 1 :(得分:0)
修正find_by
的语法并改用安全的&符
@asset = ActiveStorage::Attachment.find_by(params[:id])
@asset.purge
尝试:
@asset = ActiveStorage::Attachment.find_by(id: params[:id])
@asset&.purge