0

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

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

And the form / view in which I'm trying to delete the attachment looks like,

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>
ipatch
  • 3,933
  • 8
  • 60
  • 99

1 Answers1

0

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).

Try:

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

nathancarnes
  • 171
  • 3