3

在Rails 中使用Paperclip 的Polymorphic 版本,默认的保存技术意味着具有相同名称的文件相互覆盖。在路径和 URL 中包含 :id 不起作用,因为它只会用旧的 :id 覆盖较早的文件。

我已经尝试使用时间戳进行插值,但它只是在显示图像时查找当前时间(另外,由于我有多个缩略图,它需要超过一秒的时间,因此图像具有不同的时间戳)。

Paperclip.interpolates :uniqueid do |attachment, style|
  Time.now.to_i.to_s
end

还尝试使用十六进制数字,但它会针对每个拇指进行迭代,因此每次都有一个新的十六进制值而中断。

Paperclip.interpolates :uniqueid do |attachment, style|
  ActiveSupport::SecureRandom.hex(3)
end

由于它是多态版本,因此有自己的模型,我不知道如何从父模型(在本例中为“发布”)访问值。下面代码的变化都会引发“未定义的方法”错误。

Paperclip.interpolates :user_id do |attachment, style|
  current_user.id
end

抱歉,这似乎是一个新手问题,但对于传统的回形针来说,它是一个很好的文档,但对于多态叉来说,什么都没有。

4

1 回答 1

0

包括:class:pathfor 中has_attached_file怎么样?

has_attached_file :attachment,
  :path => ":rails_root/attachments/:class/:id/:attachment/:basename.:extension",
  :url => "downloads/:id/:title.:extension"
于 2012-03-25T22:10:43.437 回答