大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – Rails 5.2 Active Storage清除/删除附件,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
问题:出于某种原因,每当我加载集合的显示页面时,我的所有图像都会被立即清除/删除.当然,每当我点击链接时我只想删除一个文件.有谁知道如何解决这个问题?
我的收藏展示视图:
<div id="gallery"> <% @collection.images.each do |image| %> <%= image_tag(imagE) %> <%= link_to 'Remove image',image.purge %> <% end %> </div>
我已经阅读了http://edgeguides.rubyonrails.org/active_storage_overview.html#removing-files的文档
(见第4段)
但遗憾的是,这并未提供有关如何专门使用purge或purge_later方法的任何信息.
编辑
目前我的代码改为此(仍然遗憾地无效):
<div id="gallery"> <% @collection.images.each do |image| %> <%= image_tag(imagE) %> <%= link_to 'Remove',delete_image_attachment_collections_url(image.signed_id),method: :delete,data: { confirm: 'Are you sure?' } %> <% end %> </div>
在我的collections_controller.rb中有这个
def delete_image_attachment @image = ActiveStorage::Blob.find_signed(params[:id]) @image.purge redirect_to root_path end
我试图删除附加图像后,这给了我这个错误:
服务器日志:
Started deletE "/collections/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBYdz09iiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--3e75276d414b4c2040e02cf0afbc083e2337faa0/delete_image_attachment" for ::1 at 2018-03-29 19:06:55 +0200 Processing by CollectionsController#delete_image_attachment as HTML Parameters: {"authenticity_token"=>"60zIkeknxRYp/sJIWNwF+BrEftYHSCQvak34h8FkadPXgVPQSXN/sCoxI/6FU+jZbqQitES81fyqkmIx6XYP6w==","id"=>"eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBYdz09iiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--3e75276d414b4c2040e02cf0afbc083e2337faa0"} ActiveStorage::Blob Load (0.1ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2 [["id",90],["LIMIT",1]] ↳ app/controllers/collections_controller.rb:69 Disk Storage (0.1ms) deleted file from key: 8wpzqPQcWYjK2rVEejcU88FB Disk Storage (0.0ms) deleted files by key prefix: variants/8wpzqPQcWYjK2rVEejcU88FB/ (0.0ms) BEGIN ↳ app/controllers/collections_controller.rb:70 ActiveStorage::Blob Destroy (0.2ms) deletE FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 [["id",90]] ↳ app/controllers/collections_controller.rb:70 (2.0ms) COMMIT ↳ app/controllers/collections_controller.rb:70 ActiveStorage::Attachment Load (0.2ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = $1 AND "active_storage_attachments"."record_type" = $2 AND "active_storage_attachments"."name" = $3 LIMIT $4 [["record_id",["record_type","ActiveStorage::Blob"],["name","preview_image"],1]] ↳ app/controllers/collections_controller.rb:70 Redirected to http://localhost:3000/ Completed 302 Found in 5ms (ActiveRecord: 2.5ms)
耙路线的输出:
Prefix Verb URI Pattern Controller#Action root GET / home#index about GET /about(.:format) pages#about contact GET /contact(.:format) pages#contact setTings GET /setTings(.:format) setTings#edit new_setTing GET /setTing/new(.:format) setTings#new edit_setTing GET /setTing/edit(.:format) setTings#edit setTing GET /setTing(.:format) setTings#show PATCH /setTing(.:format) setTings#update PUT /setTing(.:format) setTings#update deletE /setTing(.:format) setTings#destroy POST /setTing(.:format) setTings#create delete_image_attachment_collection deletE /collections/:id/delete_image_attachment(.:format) collections#delete_image_attachment collections GET /collections(.:format) collections#index POST /collections(.:format) collections#create new_collection GET /collections/new(.:format) collections#new edit_collection GET /collections/:id/edit(.:format) collections#edit collection GET /collections/:id(.:format) collections#show PATCH /collections/:id(.:format) collections#update PUT /collections/:id(.:format) collections#update deletE /collections/:id(.:format) collections#destroy rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
我的routes.rb:
Rails.application.routes.draw do root 'home#index' get 'about',to: 'pages#about',as: :about get 'contact',to: 'pages#contact',as: :contact get 'instellingen',to: 'setTings#edit' resource :setTing resources :collections do member do delete :delete_image_attachment end end end
错误是因为图像对象返回其完整路径,并且链接认为您要指向的内容.相反,你只需要它的signed_id,并希望链接调用具有delete_image_attachment路径的路由.
<%= link_to 'Remove',data: { confirm: 'Are you sure?' } %>
destroy方法看起来像这样……
def delete_image_attachment @image = ActiveStorage::Blob.find_signed(params[:id]) @image.purge redirect_to collections_url end
路线应该是这样的……
resources :collections do member do delete :delete_image_attachment end end
查看rails routing guide以获取更多有趣的路由事实.
以上是大佬教程为你收集整理的ruby-on-rails – Rails 5.2 Active Storage清除/删除附件全部内容,希望文章能够帮你解决ruby-on-rails – Rails 5.2 Active Storage清除/删除附件所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。