移除编辑页面上的图片

huangapple go评论86阅读模式
英文:

Removing picture at editing page

问题

我正在尝试删除上传的图片,但它只是刷新页面,似乎在终端中找不到正确的图片ID,我该如何修复它?

这是我点击“删除图片”后的终端输出:

  1. 17:49:54 web.1 | 已开始 DELETE "/products/9/pictures/9",来自 127.0.0.1,时间为 2023-06-19 17:49:54 +0800
  2. 17:49:54 web.1 | 正在处理 Products::PicturesController#destroy,格式为 TURBO_STREAM
  3. 17:49:54 web.1 | 参数:{"product_id"=>"9", "id"=>"9"}
  4. 17:49:54 web.1 | 1 毫秒内完成 404 未找到(ActiveRecord: 0.0 毫秒 | 分配: 695
  5. 17:49:54 web.1 |
  6. 17:49:54 web.1 |
  7. 17:49:54 web.1 |
  8. 17:49:54 web.1 | ActiveRecord::RecordNotFound(找不到带有ID的产品):
  9. 17:49:54 web.1 |
  10. 17:49:54 web.1 | app/controllers/products/pictures_controller.rb:12:in `set_product'
  11. 17:49:54 web.1 | 已开始 GET "/products/9/edit",来自 127.0.0.1,时间为 2023-06-19 17:49:54 +0800
  12. 17:49:54 web.1 | 正在处理 ProductsController#edit,格式为 HTML
  13. 17:49:54 web.1 | 参数:{"id"=>"9"}
  14. 17:49:54 web.1 | Product Load(0.4 毫秒)SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT ? [["id", 9], ["LIMIT", 1]]
  15. 17:49:54 web.1 | ↳ app/controllers/products_controller.rb:65:in `set_product'
  16. 17:49:54 web.1 | 正在渲染布局 layouts/application.html.erb
  17. 17:49:54 web.1 | 正在渲染 products/edit.html.erb,位于 layouts/application 中
  18. 17:49:54 web.1 | ActiveStorage::Attachment Exists?(0.2 毫秒)SELECT 1 AS one FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 9], ["record_type", "Product"], ["name", "pictures"], ["LIMIT", 1]]
  19. 17:49:54 web.1 | ↳ app/views/products/_form.html.erb:27
  20. 17:49:54 web.1 | ActiveStorage::Attachment Load(0.1 毫秒)SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? [["record_id", 9], ["record_type", "Product"], ["name", "pictures"]]
  21. 17:49:54 web.1 | ↳ app/views/products/_form.html.erb:28
  22. 17:49:54 web.1 | ActiveStorage::Blob Load(0.1 毫秒)SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = ? LIMIT ? [["id", 128], ["LIMIT", 1]]
  23. 17:49:54 web.1 | ↳ app/models/product.rb:7:in `picture_as_thumbnail'
  24. 17:49:54 web.1 | ActiveStorage::VariantRecord Load0.0 毫秒)SELECT "active_storage_variant_records".* FROM "active_storage_variant_records" WHERE "active_storage_variant_records"."blob_id" = ? AND "active_storage_variant_records"."variation_digest" = ? LIMIT ? [["blob_id", 128], ["variation_digest", "6x8KFtM/8O22vFlQ4EVqBuGjN8Q="], ["LIMIT", 1]]
  25. 17:49:54 web.1 | app/models/product.rb:8:in `picture_as_thumbnail'

这是我的路由配置:

  1. Rails.application.routes.draw do
  2. root 'products#home'
  3. resources :products do
  4. resources :pictures, only: [:destroy], module: :products
  5. end
  6. # 定义你的应用程序路由,参考 https://guides.rubyonrails.org/routing.html 中的 DSL
  7. # 定义根路径路由 ("/")
  8. end

这是控制器代码:

  1. class Products::PicturesController < ApplicationController
  2. before_action :set_product
  3. def destroy
  4. @product.picture.purge_later
  5. redirect_to edit_product_path(@product)
  6. end
  7. private
  8. def set_product
  9. @product = Product.find(params[:products_id])
  10. end
  11. end

这是编辑页面中的 _form.html.erb 代码:

  1. <div>
  2. <%= form.label :pictures %>
  3. <%= form.file_field :pictures, multiple: true %>
  4. <% if @product.pictures.attached? %>
  5. <% @product.pictures.each do |picture| %>
  6. <%= link_to image_tag(product.picture_as_thumbnail(picture)), picture %>
  7. <%= link_to "remove picture", product_picture_path(product), data: { turbo_method: :delete} %>
  8. <%= form.hidden_field :pictures, multiple: true, value: picture.signed_id %>
  9. <% end %>
  10. <% end %>
  11. </div>

希望这能帮助你解决问题。

英文:

I'm trying to remove the pictures uploaded but it only refresh the page, seems like it could't find the correct picture id in terminal, how can I fix it?

This is terminal after I click "remove picture"

  1. 17:49:54 web.1 | Started DELETE &quot;/products/9/pictures/9&quot; for 127.0.0.1 at 2023-06-19 17:49:54 +0800
  2. 17:49:54 web.1 | Processing by Products::PicturesController#destroy as TURBO_STREAM
  3. 17:49:54 web.1 | Parameters: {&quot;product_id&quot;=&gt;&quot;9&quot;, &quot;id&quot;=&gt;&quot;9&quot;}
  4. 17:49:54 web.1 | Completed 404 Not Found in 1ms (ActiveRecord: 0.0ms | Allocations: 695)
  5. 17:49:54 web.1 |
  6. 17:49:54 web.1 |
  7. 17:49:54 web.1 |
  8. 17:49:54 web.1 | ActiveRecord::RecordNotFound (Couldn&#39;t find Product without an ID):
  9. 17:49:54 web.1 |
  10. 17:49:54 web.1 | app/controllers/products/pictures_controller.rb:12:in `set_product&#39;
  11. 17:49:54 web.1 | Started GET &quot;/products/9/edit&quot; for 127.0.0.1 at 2023-06-19 17:49:54 +0800
  12. 17:49:54 web.1 | Processing by ProductsController#edit as HTML
  13. 17:49:54 web.1 | Parameters: {&quot;id&quot;=&gt;&quot;9&quot;}
  14. 17:49:54 web.1 | Product Load (0.4ms) SELECT &quot;products&quot;.* FROM &quot;products&quot; WHERE &quot;products&quot;.&quot;id&quot; = ? LIMIT ? [[&quot;id&quot;, 9], [&quot;LIMIT&quot;, 1]]
  15. 17:49:54 web.1 | ↳ app/controllers/products_controller.rb:65:in `set_product&#39;
  16. 17:49:54 web.1 | Rendering layout layouts/application.html.erb
  17. 17:49:54 web.1 | Rendering products/edit.html.erb within layouts/application
  18. 17:49:54 web.1 | ActiveStorage::Attachment Exists? (0.2ms) SELECT 1 AS one FROM &quot;active_storage_attachments&quot; WHERE &quot;active_storage_attachments&quot;.&quot;record_id&quot; = ? AND &quot;active_storage_attachments&quot;.&quot;record_type&quot; = ? AND &quot;active_storage_attachments&quot;.&quot;name&quot; = ? LIMIT ? [[&quot;record_id&quot;, 9], [&quot;record_type&quot;, &quot;Product&quot;], [&quot;name&quot;, &quot;pictures&quot;], [&quot;LIMIT&quot;, 1]]
  19. 17:49:54 web.1 | app/views/products/_form.html.erb:27
  20. 17:49:54 web.1 | ActiveStorage::Attachment Load (0.1ms) SELECT &quot;active_storage_attachments&quot;.* FROM &quot;active_storage_attachments&quot; WHERE &quot;active_storage_attachments&quot;.&quot;record_id&quot; = ? AND &quot;active_storage_attachments&quot;.&quot;record_type&quot; = ? AND &quot;active_storage_attachments&quot;.&quot;name&quot; = ? [[&quot;record_id&quot;, 9], [&quot;record_type&quot;, &quot;Product&quot;], [&quot;name&quot;, &quot;pictures&quot;]]
  21. 17:49:54 web.1 | app/views/products/_form.html.erb:28
  22. 17:49:54 web.1 | ActiveStorage::Blob Load (0.1ms) SELECT &quot;active_storage_blobs&quot;.* FROM &quot;active_storage_blobs&quot; WHERE &quot;active_storage_blobs&quot;.&quot;id&quot; = ? LIMIT ? [[&quot;id&quot;, 128], [&quot;LIMIT&quot;, 1]]
  23. 17:49:54 web.1 | app/models/product.rb:7:in `picture_as_thumbnail&#39;
  24. 17:49:54 web.1 | ActiveStorage::VariantRecord Load (0.0ms) SELECT &quot;active_storage_variant_records&quot;.* FROM &quot;active_storage_variant_records&quot; WHERE &quot;active_storage_variant_records&quot;.&quot;blob_id&quot; = ? AND &quot;active_storage_variant_records&quot;.&quot;variation_digest&quot; = ? LIMIT ? [[&quot;blob_id&quot;, 128], [&quot;variation_digest&quot;, &quot;6x8KFtM/8O22vFlQ4EVqBuGjN8Q=&quot;], [&quot;LIMIT&quot;, 1]]
  25. 17:49:54 web.1 | ↳ app/models/product.rb:8:in `picture_as_thumbnail&#39;

This is my routes

  1. Rails.application.routes.draw do
  2. root &#39;products#home&#39;
  3. resources :products do
  4. resources :pictures, only: [:destroy], module: :products
  5. end
  6. # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
  7. # Defines the root path route (&quot;/&quot;)
  8. end

This is the controller

  1. class Products::PicturesController &lt; ApplicationController
  2. before_action :set_product
  3. def destroy
  4. @product.picture.purge_later
  5. redirect_to edit_product_path(@product)
  6. end
  7. private
  8. def set_product
  9. @product = Product.find(params[:products_id])
  10. end
  11. end

This is the _form.html.erb in edit page

  1. &lt;div&gt;
  2. &lt;%= form.label :pictures %&gt;
  3. &lt;%= form.file_field :pictures, multiple: true %&gt;
  4. &lt;% if @product.pictures.attached? %&gt;
  5. &lt;% @product.pictures.each do |picture| %&gt;
  6. &lt;%= link_to image_tag(product.picture_as_thumbnail(picture)), picture %&gt;
  7. &lt;%= link_to &quot;remove picture&quot;, product_picture_path(product), data: { turbo_method: :delete} %&gt;
  8. &lt;%= form.hidden_field :pictures, multiple: true, value: picture.signed_id %&gt;
  9. &lt;% end %&gt;
  10. &lt;% end %&gt;
  11. &lt;/div&gt;

答案1

得分: 1

你的问题是你使用了 params[:products_id] 而不是 params[:product_id]

如果图片是产品的附件,就像这样:

  1. class Product &lt; ApplicationRecord
  2. has_many_attached :pictures
  3. end

你可以为此目的定义一个控制器来删除任何你想要的附件:

  1. # config/routes.rb
  2. Rails.application.routes.draw do
  3. # ...
  4. resources :attachments, only: :destroy
  5. # ...
  6. end
  7. # app/controllers/attachments_controller.rb
  8. class AttachmentsController &lt; ApplicationController
  9. def destroy
  10. attachment = ActiveStorage::Attachment.find(params[:id])
  11. attachment.purge
  12. redirect_back fallback_location: root_path, status: :see_other
  13. end
  14. end

然后在视图中以这种方式渲染删除图片的按钮:

  1. &lt;% @product.pictures.each do |picture| %&gt;
  2. &lt;%= button_to &#39;Destroy picture&#39;, picture, method: :delete, form: { data: { turbo_confirm: &#39;Are you sure?&#39; } } %&gt;
  3. &lt;% end %&gt;

这样,你可以销毁应用中的任何附件,不仅仅是图片,也不仅仅是在产品中。

英文:

Your problem is that you used params[:products_id] instead of params[:product_id]

If pictures are attachments of products like this

  1. class Product &lt; ApplicationRecord
  2. has_many_attached :pictures
  3. end

you can define controller for this purpose to delete any attachment you want

  1. # config/routes.rb
  2. Rails.application.routes.draw do
  3. # ...
  4. resources :attachments, only: :destroy
  5. # ...
  6. end
  7. # app/controllers/attachments_controller.rb
  8. class AttachmentsController &lt; ApplicationController
  9. def destroy
  10. attachment = ActiveStorage::Attachment.find(params[:id])
  11. attachment.purge
  12. redirect_back fallback_location: root_path, status: :see_other
  13. end
  14. end

And render button to remove picture such way in view

  1. &lt;% @product.pictures.each do |picture| %&gt;
  2. &lt;%= button_to &#39;Destroy picture&#39;, picture, method: :delete, form: { data: { turbo_confirm: &#39;Are you sure?&#39; } } %&gt;
  3. &lt;% end %&gt;

Such way you can destroy any attachment in your app, not only pictures and not only in products

huangapple
  • 本文由 发表于 2023年6月19日 17:56:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505521.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定