开发者

How to get paperclip to delete files

I have a model that is using Paperclip to manage the file.

After I delete the model, I obviously would like the file to be deleted as 开发者_如何学运维well, but I cannot seem to find out how to get the file deleted using Paperclip.

I have tried self.sourcefile = nil if !sourcefile.dirty? in the before_destroy def, but that had no effect.

(I want to be able to have it delete the file locally when I test, and then on S3 when I use that - So i need a pure paperclip solution)

Any ideas?


Paperclip is pretty noisy when it fails to delete a file.

Without any special configuration, when you delete an object, the associated files, if any, are deleted too, and this is noted in the log (at least in development mode):

[paperclip] Deleting attachments.
[paperclip] deleting /clipper/public/system/avatars/3/original/whatever.JPG

If Paperclip is unable to delete the file, it will simply throw an exception.

All this was tested with the latest Paperclip (cloned from github on May 25th). If you have an older version you might want to update it, then run some tests while watching the logs.


I may be wrong but I believe it should do it automatically since has_attached_file adds a before_destroy callback. Source.

Edit: If it's not working, maybe you could try calling destroy_attached_files yourself, which would seem weird to me. Source.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜