Reputation: 157
I am using paperclip to save images. I have created an image model to save them to my public director
class Image < ActiveRecord::Base
has_attached_file :file,
:url => "assets/projects_description_images/:style/:basename.:extension",
:path => ":rails_root/public/assets/projects_description_images/:style/:basename.:extension"
validates_attachment :file,
:presence => true,
content_type: {content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"]},
:size => {:in => 0..50.megabytes}
end
However if I add I make an image with the name "main.jpg" and then create another with the name "main.jpg" when displaying the one that was created first it shows up with the second one. I will have no way of knowing the exact name that will be used but I can be certain that there will be duplicates. It would be great if I could have the file name be saved as something like
main_(unique_string).jpg
Any clue how to do this?
Upvotes: 0
Views: 145
Reputation: 157
The answer provided by Andrey Turkin ( trevorturk.com/2009/03/22/randomize-filename-in-paperclip)solved my problem. One thing to note is that with rails 4.2 I had to change
ActiveSupport::SecureRandom.hex(16)
to just
SecureRandom.hex(16)
Upvotes: 1