代码之家  ›  专栏  ›  技术社区  ›  Steven Smith

由于路径错误,Rails ActiveStorage无法删除本地主机上的图像

  •  0
  • Steven Smith  · 技术社区  · 6 年前

    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,因为它链接到一个用户ID的工具,我有点迷路,我希望你能找出错误

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  1
  •   arieljuod    6 年前

    听起来你有一个 before_action :set_tool except: :delete_image_attachment

    before_action :set_tool, except: :delete_image_attachment
    
    推荐文章