如何删除Rails公用文件夹中的特定文件?

Vic*_*Lam 2 ruby ruby-on-rails file public paperclip

我的网络应用中的用户可以上传文件.我使用Paperclip来处理文件附件问题.如果我想以编程方式删除任何特定的用户上传文件,有什么方法吗?

Joh*_*ley 35

Ruby的File类有一个delete方法:

File.delete(Rails.root + '/foo.jpg')
Run Code Online (Sandbox Code Playgroud)


小智 7

删除它应该就像将其设置为nil一样简单

# assuming...
has_attached_file :picture

@thing.picture = nil
@thing.save
Run Code Online (Sandbox Code Playgroud)

要么

@thing.update_attribute(:picture, nil)
Run Code Online (Sandbox Code Playgroud)

和Paperclip会为你照顾它...