2011-02-06 68 views
2

我正在製作一個小應用程序,使用回形針上傳純文本文件。我有一個Upload模型,它有一個文檔附件。我想重命名上傳的文件,使其與Upload.title相同。文件夾db /實際不匹配的回形針插值

我已經使用Paperclip插值來做到這一點。

#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" 

但是,文件本身被重命名,但在數據庫中的document_file_name保持原樣。

我做了一個測試程序,並上傳至Github上here

在這裏,我創建一個新的Upload和附加的文件 「Original File Name.txt

garethrees.co.uk/misc/new.JPG

在這裏,您會看到創建的新Upload,仍以原始文件名稱。

garethrees.co.uk/misc/created.JPG

而且也可在數據庫中,document_file_name保持原樣不變。

garethrees.co.uk/misc/db.JPG

然而,在實際的文件系統文件改名。

garethrees.co.uk/misc/finder.JPG

我真的很需要兩個記錄相匹配,因爲我需要使用回形針路徑,以供用戶下載的文件。

感謝

回答

0

創建,你自己設置document_file_name的標題+擴展after_document_post_process一個回調函數。

+0

好主意。如果我找不到原生Paperclip的解決方案,我會使用它。謝謝! – ghr 2011-02-08 09:14:38