Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 - Tempfile Path?

Tags:

I have the following:

attachments.each do |a|
   Rails.logger.info a.filename
   tempfile = Tempfile.new("#{a.filename}", "#{Rails.root.to_s}/tmp/")
   Rails.logger.info tempfile.path
end

Where attachments is from paperclip.

Here's the output:

billgates.jpg
/Users/bhellman/Sites/cline/tmp/billgates.jpg20101204-17402-of0u9o-0

Why is the file name getting 20101204-17402-of0u9o-0 appended to at the end? That's breaking everything with paperclip etc. Anyone seen this before? For the life of I have no idea what's doing it?

Thanks

UPDATE Paperclip: Paperclip on github

a is the attachment file

tempfile = Tempfile.new("#{a.filename}", "#{Rails.root.to_s}/tmp/")
tempfile << a.body
tempfile.puts
attachments.build(
    :attachment => File.open(tempfile.path)
)
like image 731
AnApprentice Avatar asked Dec 05 '10 03:12

AnApprentice


1 Answers

best make sure your tempfile has the correct extension, saving you to trying and change it after:

file = Tempfile.new(['hello', '.jpg'])

file.path # => something like: "/tmp/hello2843-8392-92849382--0.jpg"

more here: http://apidock.com/ruby/v1_9_3_125/Tempfile/new/class

like image 69
andistuder Avatar answered Nov 09 '22 09:11

andistuder