gpt4 book ai didi

ruby-on-rails - 多态回形针覆盖同名文件

转载 作者:行者123 更新时间:2023-12-04 03:45:11 24 4
gpt4 key购买 nike

在使用 Paperclip 的多态版本的 Rails 中,默认的保存技术意味着具有相同名称的文件会相互覆盖。在路径和 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

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

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

抱歉,如果这似乎是一个新手问题,但它是传统 Paperclip 的良好文档,但多态分支没有任何内容。

最佳答案

如何在has_attached_file:path 中包含:class

has_attached_file :attachment,
:path => ":rails_root/attachments/:class/:id/:attachment/:basename.:extension",
:url => "downloads/:id/:title.:extension"

关于ruby-on-rails - 多态回形针覆盖同名文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2408773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com