多态回形针插值

发布于 2024-09-04 07:58:08 字数 479 浏览 3 评论 0原文

我在 Rails 中使用 Paperclip 的多态分支,但在覆盖唯一文件名方面遇到了一些大问题。无论我是否在 URL 中添加时间戳(稍后详细介绍)或资产的 id,如果随后上传同名文件,则前一个文件将被覆盖。

此外,它之前可以工作,但时间插值现在仅输出“0”而不是时间戳。

module Paperclip
  module Interpolations
    def stamp(attachment, style)
      attachment.instance_read(:created_at).to_i
    end
  end
end

现在只是输出;

0

这就是我的 URL 字段;

:url => "/assets/images/:stamp/:id_:style.:extension"

谢谢。

I'm using the Polymorphic fork of Paperclip in Rails, but have been having some massive problems with regards to the overwriting of unique filenames. No matter whether I put a time-stamp (more on that in a second) or the id of the asset in the URL, if a file with the same name is uploaded subsequently, then the previous one is overwritten.

Also, it was working before, but the Time interpolation is now outputting just "0" instead of the timestamp.

module Paperclip
  module Interpolations
    def stamp(attachment, style)
      attachment.instance_read(:created_at).to_i
    end
  end
end

Now just outputs;

0

This is what my URL field is;

:url => "/assets/images/:stamp/:id_:style.:extension"

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桜花祭 2024-09-11 07:58:08

尝试将其添加到 config/initializers/paperclip.rb

Paperclip.interpolates :stamp do |attachment, style|
  attachment.created_at.to_i
end

Try adding this to config/initializers/paperclip.rb

Paperclip.interpolates :stamp do |attachment, style|
  attachment.created_at.to_i
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文