There have been more than a few occasions where I had images already on the same disk as a paperclip (rails paperclip plugin) attachment and I wanted to transfer those images as attachments. Here is how I do it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
begin loc_banner = "#{RAILS_ROOT}/public/images/#{File.basename(_banner_path)}" file_banner = File.new(loc_banner) temp_banner = Tempfile.new("#{Digest::SHA1.hexdigest(Time.now.to_s)}.jpg") while !file_banner.eof? temp_banner.write file_banner.read end self.banner = temp_banner #set the paperclip attachment here rescue puts "Couldn't set image #{self.id}" end |
Note that thiscode would need to require "digest/sha1"