Chris Mendla
Chris Mendla

Reputation: 1017

paperclip not deleting attachment

When I try to delete a paperclip attachment, the uploaded file remains in the folder and the record remains in the attachments table.

The document to attachment relationship is a one to many relationship. I can add attachments fine but cannot destroy the record/actual file.

Each attachment is stored in it's own folder/ i.e. public\images\21\uploadedfile.docx.

This is a slightly modified paperclip implementation in that it allows for many attachments to a document (or no attachments)

class DocumentsController < ApplicationController
  # ...
  def create
    @document = Document.new(document_params)

  respond_to do |format|

    if @document.save
      if params[:images]
        #===== The magic is here ;)
        params[:images].each { |image|
          @document.attachments.create(image: image)
        }
      end

      format.html { redirect_to @document, notice: 'Document was successfully created.' }
      format.json { render :show, status: :created, location: @document }
    else
      format.html { render :new }
      format.json { render json: @document.errors, status: :unprocessable_entity }
    end
  end
  # ...
  def destroy

    @document = Document.find(params[id])
      @document.destroy

    respond_to do |format|
      format.html { redirect_to documents_url, notice: 'Document was successfully destroyed.' }
      format.json { head :no_content }
    end
  end
  # ...
  private
    # Use callbacks to share common setup or constraints between actions.
    def set_document
      @document = Document.find(params[:id])
    end

    # refer to http://stackoverflow.com/questions/24297096/ruby-on-rails-add-fields-from-a-model-on-another-models-form 
    # for how to add the
    # permitted fields based on the .new above
    # Never trust parameters from the scary internet, only allow the white list    through.
    def document_params
      params.require(:document).permit(:subject,
                                       :body,
                                       :category_id,
                                       :tag_id,
                                       :author_id,
                                       :reviewer_id,
                                       :document_attachment,
                                       :images,
                                       :attached_files_id,
                                       :attachments,
                                       )

  end
end

   class Attachment < ActiveRecord::Base
    belongs_to :document

  ------------  Paperclip code below

  has_attached_file :image,
                    :path => ":rails_root/public/images/:id/:filename",
                    :url  => "/images/:id/:filename"

    do_not_validate_attachment_file_type :image
    \\TODO - check that the do not validate doesn't cause problems (ie uploading an exe)
  # ------------ end paperclip code

end

In the documents.show file I have tried a number of variations such as

    #    Delete this attachment?:  < %= button_to('Destroy', attachment, :method => 'destroy', onclick: 'return confirm("Are you sure?")', :class => 'btn btn-large btn-primary') %>
    Delete this attachment?:  < %=  button_to("Delete attachment", @attachment.image.destroy, onclick: 'return confirm("Are you sure?")', :class => 'btn btn-large btn-primary') %>
    <%= f.check_box :image_delete, :label => 'Delete Image' %>

what can I do to delete the record in the attachment table and also delete the corresponding attachment? thx

Upvotes: 1

Views: 537

Answers (2)

Chris Mendla
Chris Mendla

Reputation: 1017

Max, Caullou and Pavan - thanks.. After reading the replies I realized I had some sloppy code.

The solution proved to be changing the delete statement in documents\show.html.erb to

Delete this attachment?:  <%= button_to 'Delete this attachment', attachment, method: :delete, data: { confirm: 'Are you sure?' } %>

I had tried a number of variations. I ended up taking the code from the attachements\index.html.erb that was generated from the scaffold. That worked. ( I did change it from a link to a button.

Thanks again everyone!!!

Upvotes: 0

Caillou
Caillou

Reputation: 1500

You can add a dependent: :destroy to your relation between Attachment and Document, like this :

class Attachment < ActiveRecord::Base
  belongs_to :document, dependent: :destroy
  ...
end

It will automatically destroy the related Attachments when you destroy a Document.

Documentation : http://guides.rubyonrails.org/association_basics.html

Upvotes: 1

Related Questions