通过命令行连接导轨/回形针

Jer*_*iko 3 ruby-on-rails paperclip

我的服务器上的文件夹中有一堆 jpeg 文件,我试图Property通过 rake 任务将它们附加到相应的实例。

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"
Run Code Online (Sandbox Code Playgroud)

我在其他型号上使用回形针,没有任何问题,但当我尝试以下操作时遇到问题:

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."
Run Code Online (Sandbox Code Playgroud)

该文件肯定存在,并且我已尝试更改权限。重新启动服务器没有帮助。问题似乎在于使用命令行,因为正常形式/HTTP 方法工作正常。这只是一个临时设置,因此我正在寻找一种将一批文件导入我的 Rails 应用程序回形针模型的工作方法。

有什么建议么?

uki*_*ume 5

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
Run Code Online (Sandbox Code Playgroud)

  • 请参阅http://stackoverflow.com/questions/1397461/how-to-set-file-programmatically-using-paperclip (2认同)
  • .to_suym 应该是 .to_sym (2认同)