Reputation: 305
I'm having trouble getting the URL of a temp file I've created. The objective of my controller called with ajax is to generate a PDF in a temporary dir that is cleaned each time dynos start, then return JSON with the URL of the file. The JavaScript waiting for the JSON will then call window.location.href
to download the PDF.
I've done all I think of, but it's not working. I've heard that Heroku has a temporary directory cleaned each time dynos are started (cf creating-temporary-files-in-heroku). But that post is 2 years old and is talking about RAILS_ROOT
that does not exist anymore. So I'm using Rails.root.join('app', 'tmp')
Here is my controller function:
def print_a
pdf = render_to_string :pdf => params[:name],
:template => "prints/printA.pdf.html.erb",
:layout => nil
temp_dir = Rails.root.join('app','tmp')
Dir.mkdir(temp_dir) unless Dir.exists?(temp_dir)
@tempfile = Tempfile.new [params[:name], '.pdf'], temp_dir
@tempurl = "#{request.protocol}#{request.host_with_port.sub(/:80$/,"")}/tmp/#{File.basename(@tempfile.path)}"
File.open(@tempfile.path, 'wb') do |file|
file << pdf
end
render "prints/printA.rabl"
end
The JSON is rendered via prints/printA.rabl
:
node(:tempurl) { @tempurl }
node(:tempfile) { @tempfile.path }
I'm using tempfile
to see if the dir is good. It gives:
/home/alain/Projects/Heroku/pj/app/tmp/_20131022-4464-alrmkb.pdf
The real value important for the JavaScript is tempurl
:
http://0.0.0.0:5000/tmp/_20131022-4464-alrmkb.pdf
And if I visit this tempurl
, I get this error:
No route matches [GET] "/tmp/_20131022-4464-alrmkb.pdf"
Can anyone help me get it to work? Thanks.
Upvotes: 0
Views: 1169
Reputation: 18835
you can't point to the tmp folder. simple as that.
what you can do is
a) put a tmp folder in your public folder, because public is served by the http server
b) stream the file through some controller action in your rails application
Upvotes: 1