保存后的回形针重命名文件 [英] Paperclip renaming files after they're saved
问题描述
如何在上传和保存文件后重命名文件?我的问题是,我需要自动解析有关文件的信息,以便得出文件应与我的应用程序一样保存的文件名,但我无法访问生成文件名所需的信息,直到记录模型已保存.
How do I rename a file after is has been uploaded and saved? My problem is that I need to parse information about the files automatically in order to come up with the file name the file should be saved as with my application, but I can't access the information required to generate the file name till the record for the model has been saved.
推荐答案
例如,如果您的模型具有属性图像:
If, for example, your model has attribute image:
has_attached_file :image, :styles => { ...... }
默认情况下,papepclip 文件存储在/system/:attachment/:id/:style/:filename 中.
By default papepclip files are stored in /system/:attachment/:id/:style/:filename.
因此,您可以通过重命名每个样式然后更改数据库中的 image_file_name 列来完成它.
So, You can accomplish it by renaming every style and then changing image_file_name column in database.
(record.image.styles.keys+[:original]).each do |style|
path = record.image.path(style)
FileUtils.move(path, File.join(File.dirname(path), new_file_name))
end
record.image_file_name = new_file_name
record.save
这篇关于保存后的回形针重命名文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!