【问题标题】:paperclip not deleting attachment回形针不删除附件
【发布时间】:2015-10-19 12:48:21
【问题描述】:

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

文档到附件的关系是一对多的关系。我可以很好地添加附件,但不能破坏记录/实际文件。

每个附件都存储在它自己的文件夹中/即 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' %>

如何删除附件表中的记录,同时删除相应的附件?谢谢

【问题讨论】:

  • 是不是错字@document = Document.find(params[id])?如果不是,它应该是@document = Document.find(params[:id])
  • 如果一个文档有很多附件,白名单中的参数应该是attached_files_ids而不是attached_files_id。当你应该调用Document.find(params[:id]) - the former will call an method on the controller called id`(如果存在)时,你也在调用Document.find(params[id])
  • 我已经清理了你的控制器代码 - 请清理剩余的代码。一个很好的提示是不要写“这是我的用户控制器”——而是包含类定义——这样更容易阅读。

标签: ruby-on-rails ruby paperclip


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

    Max、Caullou 和 Pavan - 谢谢.. 阅读回复后,我意识到我的代码有些草率。

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

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

    我尝试了多种变体。我最终从脚手架生成的 attachements\index.html.erb 中获取了代码。那行得通。 (我确实将其从链接更改为按钮。

    再次感谢大家!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      • 2011-06-14
      相关资源
      最近更新 更多