英文:
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 "/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'
This is my routes
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
This is the controller
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
This is the _form.html.erb in edit page
<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>
答案1
得分: 1
你的问题是你使用了 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 %>
这样,你可以销毁应用中的任何附件,不仅仅是图片,也不仅仅是在产品中。
英文:
Your problem is that you used params[:products_id]
instead of params[:product_id]
If pictures are attachments of products like this
class Product < 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 < 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
<% @product.pictures.each do |picture| %>
<%= button_to 'Destroy picture', picture, method: :delete, form: { data: { turbo_confirm: 'Are you sure?' } } %>
<% end %>
Such way you can destroy any attachment in your app, not only pictures and not only in products
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论