简体   繁体   English

如何使用重新归档ActiveRecord :: RecordNotFound删除附件

[英]How to delete attachment using refile ActiveRecord::RecordNotFound

I'm trying to delete / destroy an attachment I have uploaded using the refile gem, and I'm currently getting the following error, ActiveRecord::RecordNotFound in ImagesController#destroy 我正在尝试删除/销毁使用重文件gem上传的附件,并且当前出现以下错误, ActiveRecord::RecordNotFound in ImagesController#destroy

Couldn't find Image with 'id'=

images_controller.rb images_controller.rb

def destroy
        # binding.pry
        @image = Image.find(params[:id])
        @image.destroy
        flash[:notice] = 'deleted'
        render 'new'
    end

    private

    def image_params
        params.require(:image).permit(:tshirt_image, :remove_tshirt_image)
    end

And the form / view in which I'm trying to delete the attachment looks like, 我试图删除附件的表单/视图如下所示:

show.html.erb show.html.erb

<h1>show.html.erb</h1>

<% debug(@image) %>

<%= image_tag attachment_url(@image, :tshirt_image, :fill, 300, 300, format: "jpg") %>

<%= form_for @image do |form| %>
  <%= form.label :tshirt_image %>
  <% form.attachment_field :tshirt_image %>

  <%= form.check_box :remove_tshirt_image %>
  <%= form.label :remove_tshirt_image %>

  <%= button_to "delete", @image, method: :delete %>


<% end %>

<p>test</p>

If I'm reading your code correctly, all of your form's params will be nested under the :image key ( params[:image][:remove_tshirt_image] , etc). 如果我正确地阅读了您的代码,则表单的所有参数都将嵌套在:image键下( params[:image][:remove_tshirt_image]等)。

Try: 尝试:

@image = Image.find(params[:image][:id])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM