2011-05-24 45 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