パスの指定と、ディレクトリを作成することがポイント。
まず、Tempfileのデフォルトパスでは作れないのでパスを指定して作ることにする
Herokuではtmp
もしくはlogs
にだけ書き込める
temp_file = Tempfile.new(prefix, Rails.root.join('tmp'))
さらにRails.root.join('tmp')
は自動で作成されないので、チェックしてない場合は作成する必要がある。
temp_dir = Rails.root.join('tmp')
Dir.mkdir(temp_dir) unless Dir.exists?(temp_dir)
参考
No such file or directory error with ruby tempfile and heroku cedar | kulehandluke