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

时间:2019-03-20 12:24:37

标签: ruby-on-rails rails-activestorage

我正在与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,我迷路了,希望您能查明错误

enter image description here

1 个答案:

答案 0 :(得分:1)

听起来像您有before_action :set_tool,请添加选项except: :delete_image_attachment

before_action :set_tool, except: :delete_image_attachment