Search code examples
ruby-on-railsrubytemporary-files

Rails: Tempfile instances garbage collected before view loaded


For my Rails app, I have a controller that generates a single-use temporary image that the corresponding view needs to load. I am currently using Tempfile for this, but the problem is that the file is sometimes garbage collected before the view has a chance to load it.

I have considered using File with the Maid gem or a cron job to clean up the images on a periodic basis, but would prefer a cleaner solution.

For example, is there a built-in page load callback concept in Rails that would allow me to call a helper method after the view finishes rendering?


Solution

  • In the end, I decided to encode my image in base64 and embed it in the view.