我正在与ActiveStorage
一起工作,并且能够上传图像并进行显示而不会出现问题,但是无法清除或删除其中一张图像。
这是控制器代码:
def photo_upload
@images = @tool.images
end
def delete_image_attachment
@images = ActiveStorage::Attachment.find(params[:id])
@images.purge
redirect_back(fallback_location: request.referer)
end
private
def set_tool
@tool = Tool.find(params[:id])
end
def is_authorised
redirect_to root_path, alert: "You don't have permission" unless current_user.id == @tool.user_id
end
def tool_params
params.require(:tool).permit(:tool_type, :power_type, :accessories, :brand, :listing_name, :summary, :address, :is_machine,
:is_hand, :is_cordless, :is_cord, :price, :active ,images: [])
end
end
...和.erb
文件
<% if @tool.images.attached? %>
<div class="panel panel-default">
<div class="panel-heading">
Photo Display
</div>
<br>
<div class="row">
<% @tool.images.each do |image| %>
<div class="col-md-4">
<div class="panel panel-default tools-gallery-panel">
<div class="panel-heading preview ">
<%= link_to image_tag(image, class:"img-thumbnail tools-gallery"), image %>
</div>
<div class="panel-body">
<span class="pull-right">
<i class="fa fa-trash-o fa-lg" aria-hidden="true"></i>
<%= link_to "Remove", delete_image_attachment_tool_path(image), method: :delete, data: {confirm: "Are you sure?"} %>
</span>
</div>
</div>
</div>
<% end %>
<% end %>
</div>
我得到一个错误,因为它是错误的ID,因为它与tools_id链接到用户ID,我迷路了,希望您能查明错误
答案 0 :(得分:1)
听起来像您有before_action :set_tool
,请添加选项except: :delete_image_attachment
before_action :set_tool, except: :delete_image_attachment