In the end, I decided to encode my image in base64 and embed it in the view.
Rails: Tempfile instances garbage collected before view loaded
-
03-06-2022 - |
質問
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?
解決 2
他のヒント
You can use after_filter
s in your controller to do any cleanup:
class YourController < ApplicationController
after_filter :cleanup
private
def cleanup
# clean it up
end
end
所属していません StackOverflow