【问题标题】:Rails / Paperclip attaching via command line通过命令行附加 Rails / Paperclip
【发布时间】:2011-05-24 10:39:36
【问题描述】:

我的服务器上的一个文件夹中有一堆 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 应用程序回形针模型的工作方法。

有什么建议吗?

【问题讨论】:

  • 看看这个answer
  • 不,我已经尝试过了,它似乎没有任何区别。正如我所说,当我通过 apache 添加图像时它工作正常 - 问题出在 rake 任务/控制台中。
  • 您确定图片是正确的吗?如果您手动调用identify /tmp/stream2011.... 会发生什么?也许一些较新的版本对你有用 - 我可以毫无问题地使用回形针 2.4.0 导入文件

标签: ruby-on-rails paperclip


【解决方案1】:
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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-10
  • 2013-02-25
相关资源
最近更新 更多