ruby-on-rails 在编辑页面时删除图片

eyh26e7m  于 2023-06-25  发布在  Ruby
关注(0)|答案(1)|浏览(157)

我试图删除上传的图片,但它只是刷新页面,似乎它无法找到正确的图片id在终端,我该如何修复它?
这是我点击“删除图片”后的终端

17:49:54 web.1  | Started DELETE "/products/9/pictures/9" 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: {"product_id"=>"9", "id"=>"9"}
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'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'
17:49:54 web.1  | Started GET "/products/9/edit" 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: {"id"=>"9"}
17:49:54 web.1  |   Product Load (0.4ms)  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  |   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 "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.1ms)  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.1ms)  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.0ms)  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
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Defines the root path route ("/")
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>
u1ehiz5o

u1ehiz5o1#

您的问题是使用了params[:products_id]而不是params[:product_id]
如果图片是这样的产品的附件

class Product < 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 < ApplicationController
  def destroy
    attachment = ActiveStorage::Attachment.find(params[:id])
    attachment.purge
    redirect_back fallback_location: root_path, status: :see_other
  end
end

和渲染按钮,以删除图片这样的方式在视图中

<% @product.pictures.each do |picture| %>
  <%= button_to 'Destroy picture', picture, method: :delete, form: { data: { turbo_confirm: 'Are you sure?' } } %>
<% end %>

这样你就可以销毁任何附件在您的应用程序,不仅图片,不仅在产品

相关问题