Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
908 views
in Technique[技术] by (71.8m points)

ruby on rails - How to make carrierwave delete the file when destroying a record?

I'm using the carrierwave gem to upload files.

I have built a system for users to flag images as inappropriate and for admins to remove the images. From what I can tell, calling destroy on the image will only remove the path name from the table.

Is there a way to have carrierwave actually remove the file itself? Or should rails automatically remove the file when I destroy the image path?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Like @mu_is_too_short said, you can use File#delete.

Here's a code snippet you could use as a helper with a little tweaking in your rails app.

def remove_file(file)
  File.delete(file)
end

or if you just have the filename stored in file

def remove_file(file)
  File.delete("./path/to/#{file}")
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...