2011-05-24 44 views
3

我的服務器上有一堆jpeg文件,我試圖通過rake任務將它們附加到相應的Property實例上。通過命令行附加滑軌/回形針

property.rb具有下面的代碼:

has_attached_file :temp_photo, 
    :styles => PropertyImage::STYLES, 
    :url => "/assets/:class/:attachment/:id_partition/:style_:basename.:extension", 
    :path => "#{Rails.root}/public/assets/:class/:attachment/:id_partition/:style_:basename.:extension" 

我用回形針其他型號,並沒有任何問題任何,但我得到一個問題,當我嘗試以下操作:

p = Property.find(id) 
file = File.open(temp_file_path) 
p.temp_photo = file 
p.save 

# => false 

file.close 
p.errors 

# => "/tmp/stream20110524-1126-1cunv0y-0.jpg is not recognized by the 'identify' command." 

的文件肯定存在,並且我嘗試更改權限。重新啓動服務器沒有幫助。問題似乎與使用命令行,因爲正常的形式/ HTTP方法工作正常。這只是一個臨時設置,所以我正在尋找一種工作方式將一批文件導入到我的Rails應用程序回形針模型中。

有什麼建議嗎?

+0

看看這個【答案】(http://stackoverflow.com/questions/1996102/rails-paperclip-and-passenger-is-not-recognized-by-the-identify-command) – sled 2011-05-24 10:45:41

+0

不,我已經試過了,這似乎沒有什麼區別。正如我所說的,當我通過apache添加圖像時它工作正常 - 問題出在rake任務/控制檯中。 – Jeriko 2011-05-24 11:08:04

+1

你確定圖像是正確的嗎?如果您手動調用'identify/tmp/stream2011 ....',會發生什麼情況?也許一些更新的版本將爲你工作 - 我可以沒有問題用回形針導入文件2.4.0 – Arsen7 2011-09-12 14:56:12

回答

4
path = 'target_file_path' 
attach_name = 'temp_photo' 

p = Property.find(id) 
attach = Paperclip::Attachment.new(attach_name, p, p.class.attachment_definitions[attach_name.to_suym]) 

file = File.open(path) 
attach.assign file 
attach.save 

file.close 
+2

請參閱http://stackoverflow.com/questions/1397461/how-to-set-file-programmatically-using-paperclip – 2012-08-14 08:03:15

+0

.to_suym應該是.to_sym – 2016-12-15 14:52:50