回形针,如何在文件末尾附加一个随机图章? [英] Paperclip, how to append a random stamp at the end of the file?
本文介绍了回形针,如何在文件末尾附加一个随机图章?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 Rails 3 应用中使用回形针.我想在文件末尾附加一个随机字符串,不要太长或太疯狂来缓存 CDN.有谁知道一个真正简单的方法来做到这一点?
I'm using paperclip with my rails 3 app. I want to append a random string, nothing to long or crazy at the end of the file to cache bust the CDN. Anyone know a real simple way to do this?
这是我目前拥有的:
has_attached_file :photo,
:styles => { :thumb => "70x70>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:rails_env/public/users/:id/:style/:basename.:extension",
.....
我想要一个像 FILENAME_31313.png 这样的文件名
I would like a file name like FILENAME_31313.png
每次保存照片时 31313 都是随机的.
Where 31313 is random every time a photo is saved.
谢谢
推荐答案
你可以使用这样的东西来完成工作:
You can use something like this to get the job done:
before_create :generate_random_hex
private
def generate_random_hex
self.random_hex = ActiveSupport::SecureRandom.hex(8)
end
Paperclip.interpolates :random_hex do |attachment, style|
attachment.instance.random_hex
end
然后像这样修改你的回形针设置:
Then modify your paperclip settings like so:
has_attached_file :photo,
:styles => { :thumb => "70x70>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => "/:rails_env/public/users/:id/:style/:basename_:random_hex.:extension",
这篇关于回形针,如何在文件末尾附加一个随机图章?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文