2010-12-15 51 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?