回形针不删除附件

时间:2015-10-19 12:48:21

标签: ruby-on-rails ruby paperclip

当我尝试删除回形针附件时,上传的文件仍保留在文件夹中,并且记录仍保留在附件表中。

文档到附件关系是一对多的关系。我可以添加附件,但不能销毁记录/实际文件。

每个附件都存储在其自己的文件夹/即public \ images \ 21 \ uploadedfile.docx中。

这是一个略微修改的回形针实现,因为它允许文档的许多附件(或没有附件)

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

在documents.show文件中,我尝试了很多变体,例如

    #    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' %>

如何删除附件表中的记录并删除相应的附件? THX

2 个答案:

答案 0 :(得分:1)

您可以在“附件”和“文档”之间的关系中添加dependent: :destroy,如下所示:

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

销毁文档时,它会自动销毁相关的附件。

文档:http://guides.rubyonrails.org/association_basics.html

答案 1 :(得分:0)

Max,Caullou和Pavan - 谢谢......在看完回复后,我意识到我有一些草率的代码。

解决方案证明是将documents \ show.html.erb中的delete语句更改为

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

我尝试了很多变化。我最终获取了从scaffold生成的attachements \ index.html.erb中的代码。那很有效。 (我确实将其从链接更改为按钮。

再次感谢大家!!!