Rails - Using Tempfile to write on Heroku?
I need to be able to write a temporary file for use during the request only.
Locally I can use the following successfully:
tempfile = File.open(a.original_filename,'w')
tempfile.write_nonblock(a.body)
paperclip stuff........
tempfile.close
That works great, but not on Heroku... How can I do the above with Heroku's restri开发者_开发知识库ctions: link text
I'm not understanding how to translate the above into: #{RAILS_ROOT}/tmp/myfile_#{Process.pid}
Thanks for any help you can provide here.
Did you try tempfile = File.open("#{RAILS_ROOT}/tmp/myfile_#{Process.pid}",'w')
?
The correct syntax is tempfile = File.new("#{RAILS_ROOT}/tmp/myfile_#{Process.pid}",'w')
(see comments)
I have a solution for you if you have to do stuff with paperclip. Include this class in your lib folder as heroku_compliant_file.rb
class HerokuCompliantFile < StringIO
def initialize(str,filename,content_type)
@original_filename = filename
@content_type = content_type
super(str)
end
end
In your model containing the paperclip -
def insert_a_hunk_of_string_into_paperclip(some_object)
f = HerokuCompliantFile.new(some_object.render,"myfile_#{Process.pid}","application/pdf")
self.paperclip_model = f
save
end
精彩评论