Paperclip interpolation for filename db/actual mis-match
I'm making a small app to upload plain text files using Paperclip. I have an Upload
model that has a document attachment. I want to rename the uploaded file so that it is the same as Upload.title
.
I've used a Paperclip 开发者_JAVA技巧interpolation to do this.
#config/initializers/paperclip.rb
Paperclip.interpolates('upload_title') do |attachment, style|
attachment.instance.title.parameterize
end
#app/models/upload.rb
has_attached_file :document,
:url => "/:attachment/:id/:upload_title.:extension",
:path => ":rails_root/public/:attachment/:id/:upload_title.:extension"
However, the file itself is renamed but the document_file_name
in the database remains as it was.
I've made a test app and uploaded to github here
Here I create a new Upload
and attach the file "Original File Name.txt
"
garethrees.co.uk/misc/new.JPG
Here you see the new Upload
created, still with the original file name.
garethrees.co.uk/misc/created.JPG
And also in the database, the document_file_name
remains the same as it was.
garethrees.co.uk/misc/db.JPG
However, in the actual filesystem the document is renamed.
garethrees.co.uk/misc/finder.JPG
I really need both records to match as I need to use the Paperclip path in order for users to download the files.
Thanks
create a callback function for after_document_post_process where you set the document_file_name yourself to the title + extension.
精彩评论