views:

24

answers:

2

User in my web app are able to upload file. I use Paperclip to handle file attachment issue. Is there any method if I would like to remove any specific user-uploaded file programmatically?

+1  A: 

Ruby's File class has a delete method:

File.delete(Rails.root + '/foo.jpg')
John Topley
Thanks buddy. :)
siulamvictor
+2  A: 

Deleting it should be as simple as setting it to nil

# assuming...
has_attached_file :picture

@thing.picture = nil
@thing.save

or

@thing.update_attribute(:picture, nil)

and Paperclip will take care of it for you...

mylescarrick
Thanks. I overlooked in Paperclip Readme and couldn't figure it out before. >.<
siulamvictor
I like to define a delete_thing= method that looks for typical boolean form return values and deletes the photo if what's passed in is true.def delete_thing=(val) thing = nil if val =~ /true/on/1/; end
Jared