Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails route to access files saved to tmp directory

In my background worker I'm creating a pdf and saving it to the tmp directory with:

save_path = Rails.root.join('tmp', unique_report_name)
File.open(save_path, 'wb') do |file|
  file << pdf
end

I then try to open the pdf in a new window and get ActionController::RoutingError (No route matches [GET] errors.

I've tried copying what they do with CarrierWave (I'm not using CarrierWave) by using the below in config.ru:

use Rack::Static, :urls => ['/tmp'], :root => 'tmp'

but I get the error File not found: /tmp/my_file_name.pdf even though the file does indeed exist in the location.

like image 241
Marklar Avatar asked Jan 01 '26 00:01

Marklar


1 Answers

You should be able to reach that file using Rails.root

"#{Rails.root}/tmp/my_file_name.pdf"
like image 148
Dmytro Kharytonenko Avatar answered Jan 02 '26 12:01

Dmytro Kharytonenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!