移除编辑页面上的图片

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

Removing picture at editing page

问题

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

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

17:49:54 web.1  | 已开始 DELETE "/products/9/pictures/9",来自 127.0.0.1,时间为 2023-06-19 17:49:54 +0800
17:49:54 web.1  | 正在处理 Products::PicturesController#destroy,格式为 TURBO_STREAM
17:49:54 web.1  | 参数:{"product_id"=>"9", "id"=>"9"}
17:49:54 web.1  | 在 1 毫秒内完成 404 未找到(ActiveRecord: 0.0 毫秒 | 分配: 695)
17:49:54 web.1  |
17:49:54 web.1  |
17:49:54 web.1  |
17:49:54 web.1  | ActiveRecord::RecordNotFound(找不到带有ID的产品):
17:49:54 web.1  |
17:49:54 web.1  | app/controllers/products/pictures_controller.rb:12:in `set_product'
17:49:54 web.1  | 已开始 GET "/products/9/edit",来自 127.0.0.1,时间为 2023-06-19 17:49:54 +0800
17:49:54 web.1  | 正在处理 ProductsController#edit,格式为 HTML
17:49:54 web.1  | 参数:{"id"=>"9"}
17:49:54 web.1  | Product Load(0.4 毫秒)SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT ?  [["id", 9], ["LIMIT", 1]]
17:49:54 web.1  |   ↳ app/controllers/products_controller.rb:65:in `set_product'
17:49:54 web.1  | 正在渲染布局 layouts/application.html.erb
17:49:54 web.1  | 正在渲染 products/edit.html.erb,位于 layouts/application 中
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]]
17:49:54 web.1  |   ↳ app/views/products/_form.html.erb:27
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"]]
17:49:54 web.1  |   ↳ app/views/products/_form.html.erb:28
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]]
17:49:54 web.1  |   ↳ app/models/product.rb:7:in `picture_as_thumbnail'
17:49:54 web.1  | ActiveStorage::VariantRecord Load(0.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]]
17:49:54 web.1  |   ↳ app/models/product.rb:8:in `picture_as_thumbnail'

这是我的路由配置:

Rails.application.routes.draw do
  root 'products#home'
  resources :products do 
    resources :pictures, only: [:destroy], module: :products
  end
  # 定义你的应用程序路由,参考 https://guides.rubyonrails.org/routing.html 中的 DSL

  # 定义根路径路由 ("/")
end

这是控制器代码:

class Products::PicturesController < ApplicationController
  before_action :set_product

  def destroy
    @product.picture.purge_later
    redirect_to edit_product_path(@product)
  end

  private

  def set_product
    @product = Product.find(params[:products_id])
  end
end

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

<div>
    <%= form.label :pictures %>
    <%= form.file_field :pictures, multiple: true %>
    <% if @product.pictures.attached? %>
      <% @product.pictures.each  do |picture| %>
        <%= link_to image_tag(product.picture_as_thumbnail(picture)), picture %>
        <%= link_to "remove picture", product_picture_path(product), data: { turbo_method: :delete} %>
        <%= form.hidden_field :pictures, multiple: true, value: picture.signed_id %>
      <% end %>
    <% end  %>
  </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"

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
17:49:54 web.1  | Processing by Products::PicturesController#destroy as TURBO_STREAM
17:49:54 web.1  |   Parameters: {&quot;product_id&quot;=&gt;&quot;9&quot;, &quot;id&quot;=&gt;&quot;9&quot;}
17:49:54 web.1  | Completed 404 Not Found in 1ms (ActiveRecord: 0.0ms | Allocations: 695)
17:49:54 web.1  |
17:49:54 web.1  |
17:49:54 web.1  |
17:49:54 web.1  | ActiveRecord::RecordNotFound (Couldn&#39;t find Product without an ID):
17:49:54 web.1  |
17:49:54 web.1  | app/controllers/products/pictures_controller.rb:12:in `set_product&#39;
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
17:49:54 web.1  | Processing by ProductsController#edit as HTML
17:49:54 web.1  |   Parameters: {&quot;id&quot;=&gt;&quot;9&quot;}
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]]
17:49:54 web.1  |   ↳ app/controllers/products_controller.rb:65:in `set_product&#39;
17:49:54 web.1  |   Rendering layout layouts/application.html.erb
17:49:54 web.1  |   Rendering products/edit.html.erb within layouts/application
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]]
17:49:54 web.1  |   ↳ app/views/products/_form.html.erb:27
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;]]
17:49:54 web.1  |   ↳ app/views/products/_form.html.erb:28
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]]
17:49:54 web.1  |   ↳ app/models/product.rb:7:in `picture_as_thumbnail&#39;
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]]
17:49:54 web.1  |   ↳ app/models/product.rb:8:in `picture_as_thumbnail&#39;

This is my routes

Rails.application.routes.draw do
  root &#39;products#home&#39;
  resources :products do 
    resources :pictures, only: [:destroy], module: :products
  end
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Defines the root path route (&quot;/&quot;)
end

This is the controller

class Products::PicturesController &lt; ApplicationController
  before_action :set_product

  def destroy
    @product.picture.purge_later
    redirect_to edit_product_path(@product)
  end

  private

  def set_product
    @product = Product.find(params[:products_id])
  end
end

This is the _form.html.erb in edit page

&lt;div&gt;
    &lt;%= form.label :pictures %&gt;
    &lt;%= form.file_field :pictures, multiple: true %&gt;
    &lt;% if @product.pictures.attached? %&gt;
      &lt;% @product.pictures.each do |picture| %&gt;
        &lt;%= link_to image_tag(product.picture_as_thumbnail(picture)), picture %&gt;
        &lt;%= link_to &quot;remove picture&quot;, product_picture_path(product), data: { turbo_method: :delete} %&gt;
        &lt;%= form.hidden_field :pictures, multiple: true, value: picture.signed_id %&gt;
      &lt;% end %&gt;
    &lt;% end %&gt;
  &lt;/div&gt;

答案1

得分: 1

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

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

class Product &lt; ApplicationRecord
  has_many_attached :pictures
end

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

# config/routes.rb
Rails.application.routes.draw do
  # ...
  resources :attachments, only: :destroy
  # ...
end

# app/controllers/attachments_controller.rb
class AttachmentsController &lt; ApplicationController
  def destroy
    attachment = ActiveStorage::Attachment.find(params[:id])
    attachment.purge
    redirect_back fallback_location: root_path, status: :see_other
  end
end

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

&lt;% @product.pictures.each do |picture| %&gt;
  &lt;%= button_to &#39;Destroy picture&#39;, picture, method: :delete, form: { data: { turbo_confirm: &#39;Are you sure?&#39; } } %&gt;
&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

class Product &lt; ApplicationRecord
  has_many_attached :pictures
end

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

# config/routes.rb
Rails.application.routes.draw do
  # ...
  resources :attachments, only: :destroy
  # ...
end

# app/controllers/attachments_controller.rb
class AttachmentsController &lt; ApplicationController
  def destroy
    attachment = ActiveStorage::Attachment.find(params[:id])
    attachment.purge
    redirect_back fallback_location: root_path, status: :see_other
  end
end

And render button to remove picture such way in view

&lt;% @product.pictures.each do |picture| %&gt;
  &lt;%= button_to &#39;Destroy picture&#39;, picture, method: :delete, form: { data: { turbo_confirm: &#39;Are you sure?&#39; } } %&gt;
&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:

确定