2010-12-15 62 views
5

我需要能够从S3中删除用户存储的文件,例如个人资料照片。只是打电话@user.logo.destroy似乎没有办法 - 我在日志中获得[paperclip] Saving attachments.,并且文件保留在S3存储桶中。回形针 - 从Amazon S3中删除文件?

如何删除文件本身?

回答

2

这是从回形针的方法可以用来删除附件:

module Paperclip 
    class Attachment 
    # Clears out the attachment. Has the same effect as previously assigning 
    # nil to the attachment. Does NOT save. If you wish to clear AND save, 
    # use #destroy. 
    def clear 
     queue_existing_for_delete 
     @errors   = {} 
    end 

    # Destroys the attachment. Has the same effect as previously assigning 
    # nil to the attachment *and saving*. This is permanent. If you wish to 
    # wipe out the existing attachment but not save, use #clear. 
    def destroy 
     clear 
     save 
    end 

所以你看,摧毁只有当没有出现错误删除附件。我已经用我自己的设置对S3进行了尝试,所以我知道摧毁工程。

问题在你的情况下可能是你有任何验证,取消保存? I.e validates_attachment_presence或类似的东西?

我想找出一种方法是尝试@ user.logo.destroy,然后检查@ user.errors的内容以查看它是否报告任何错误消息。

+0

我做你在这里列出,你能看到我的问题在这里:http://stackoverflow.com/questions/14144454/how-to-hook-for-destroy-of-a-model-that-belongs-到另一个模型 – simo 2013-01-04 08:26:26

1

这似乎是一个回答你的问题,虽然我不完全理解破坏和清除它们之间的区别(我不知道是哪个型号has_attached_file,页面或图像):

Rails Paperclip how to delete attachment?