单击 link 以删除 Active Storage 和 Amazon S3 上的附件
Click a link to remove an attachment on Active Storage & Amazon S3
是否有人创建了一个表单,用户可以通过单击按钮从 Active Storage 和 Amazon S3 中删除之前上传的图像?我使用问题 作为指导,但我的应用程序设置有点不同。图像保存为数组(请参阅控制器参数)。
表单呈现删除按钮和图像,但是当单击删除按钮时我收到错误 "Couldn't find Space with 'id'=eyJfcmFpbHM..." 并且我的 set_space 方法中的这一行被突出显示
@space = Space.find(params[:id])
这里是相关代码
控制器
class SpacesController < ApplicationController
before_action :set_space, except: [:index, :new, :create]
before_action :authenticate_user!, except: [:show]
def update
if @space.update(space_params)
flash[:notice] = "Saved!"
else
flash[:notice] = "Something went wrong. Please check your submission and try again."
end
redirect_back(fallback_location: request.referer)
end
def delete_image_attachment
@space_image = ActiveStorage::Blob.find_signed(params[:id])
@space_image.purge_later
redirect_to listing_space_path(@space)
end
private
def set_space
@space = Space.find(params[:id])
end
def space_params
params.require(:space).permit(:space_name, :space_type, :description, space_image: [])
end
end
带有删除按钮/图标的视图
<div>
<% if @space.image.attached? %>
<% @space.image.each do |image| %>
<%= image_tag image %>
<span>
<%= link_to '<- Remove', delete_image_attachment_space_url(image.signed_id),
method: :delete,
data: { confirm: 'Are you sure?' } %>
<i class="fas fa-trash"></i>
</span>
<% end %>
<% end %>
</div>
Routes.rb
resources :spaces, except: [:edit] do
member do
get 'listing'
delete :delete_image_attachment
end
end
set_space
正在寻找 Space 对象的 ID
对 delete_image_attachment
的调用正在传递 image.signed_id
,SpaceImage 对象的 id
,而不是 id
对于 Space 对象。
假设已在 Space 和 SpaceImage 上以标准方式设置导航类,space对象可以从图像对象中找到。所以做出这些改变...
before_action :set_space, except: [:index, :new, :create, :delete_image_attachment]
def delete_image_attachment
@space_image = ActiveStorage::Blob.find_signed(params[:id])
@space_image.purge_later
redirect_to listing_space_path(@space_image.space)
end
这会将正确的 space id 传递给 listing_space_path。
是否有人创建了一个表单,用户可以通过单击按钮从 Active Storage 和 Amazon S3 中删除之前上传的图像?我使用问题
表单呈现删除按钮和图像,但是当单击删除按钮时我收到错误 "Couldn't find Space with 'id'=eyJfcmFpbHM..." 并且我的 set_space 方法中的这一行被突出显示
@space = Space.find(params[:id])
这里是相关代码
控制器
class SpacesController < ApplicationController
before_action :set_space, except: [:index, :new, :create]
before_action :authenticate_user!, except: [:show]
def update
if @space.update(space_params)
flash[:notice] = "Saved!"
else
flash[:notice] = "Something went wrong. Please check your submission and try again."
end
redirect_back(fallback_location: request.referer)
end
def delete_image_attachment
@space_image = ActiveStorage::Blob.find_signed(params[:id])
@space_image.purge_later
redirect_to listing_space_path(@space)
end
private
def set_space
@space = Space.find(params[:id])
end
def space_params
params.require(:space).permit(:space_name, :space_type, :description, space_image: [])
end
end
带有删除按钮/图标的视图
<div>
<% if @space.image.attached? %>
<% @space.image.each do |image| %>
<%= image_tag image %>
<span>
<%= link_to '<- Remove', delete_image_attachment_space_url(image.signed_id),
method: :delete,
data: { confirm: 'Are you sure?' } %>
<i class="fas fa-trash"></i>
</span>
<% end %>
<% end %>
</div>
Routes.rb
resources :spaces, except: [:edit] do
member do
get 'listing'
delete :delete_image_attachment
end
end
set_space
正在寻找 Space 对象的 ID
对 delete_image_attachment
的调用正在传递 image.signed_id
,SpaceImage 对象的 id
,而不是 id
对于 Space 对象。
假设已在 Space 和 SpaceImage 上以标准方式设置导航类,space对象可以从图像对象中找到。所以做出这些改变...
before_action :set_space, except: [:index, :new, :create, :delete_image_attachment]
def delete_image_attachment
@space_image = ActiveStorage::Blob.find_signed(params[:id])
@space_image.purge_later
redirect_to listing_space_path(@space_image.space)
end
这会将正确的 space id 传递给 listing_space_path。