如何使用refile ActiveRecord :: RecordNotFound删除附件

时间:2016-01-16 23:09:36

标签: ruby-on-rails refile

我正在尝试使用refile gem删除/销毁我上传的附件,我目前收到以下错误, ActiveRecord::RecordNotFound in ImagesController#destroy

Couldn't find Image with 'id'=

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

我正在尝试删除附件的表单/视图,

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>

1 个答案:

答案 0 :(得分:0)

如果我正确阅读您的代码,您的所有表单参数将嵌套在:image键(params[:image][:remove_tshirt_image]等)下。

尝试:

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