Rails - 使用Tempfile在Heroku上编写? [英] Rails - Using Tempfile to write on Heroku?

查看:109
本文介绍了Rails - 使用Tempfile在Heroku上编写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



本地我可以成功地使用以下内容:

  tempfile = File.open(a.original_filename,'w')
tempfile.write_nonblock(a.body)
paperclip stuff。 .......
tempfile.close

这很好,但不是Heroku ...我如何用Heroku的限制来完成上述操作:



我不理解如何将上述内容翻译为:#{RAILS_ROOT} / tmp / myfile _#{Process.pid}
感谢您提供的任何帮助。

解决方案

我有一个解决方案,如果你必须做回形针的东西。将这个类作为heroku_compliant_file.rb包含在你的lib文件夹中

  class HerokuCompliantFile< StringIO 
def初始化(str,filename,content_type)
@original_filename =文件名
@content_type = content_type
超级(str)
结束
结束

在包含回形针的模型中 -

  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
保存
结束


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 restrictions: 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.

解决方案

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

这篇关于Rails - 使用Tempfile在Heroku上编写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆