Rails 由于路径错误,ActiveStorage 无法删除本地主机上的图像
Rails ActiveStorage unable to remove images on the localhost due to wrong path
我正在使用 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>
我收到一个错误,因为它链接到 tools_id 到用户 ID 时 ID 错误,我有点迷路了,我希望你能指出错误
听起来你有 before_action :set_tool
,添加选项 except: :delete_image_attachment
before_action :set_tool, except: :delete_image_attachment
我正在使用 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>
我收到一个错误,因为它链接到 tools_id 到用户 ID 时 ID 错误,我有点迷路了,我希望你能指出错误
听起来你有 before_action :set_tool
,添加选项 except: :delete_image_attachment
before_action :set_tool, except: :delete_image_attachment