Serving dynamically generated images in Rails 6.1

127 Views Asked by At

My application dynamically generates images using the gruff gem and then serves these images. The images are stored in app/assets/images/gruff. The images are altered during runtime (keeping the same filename but altering their contents), since these images contain a bar graph containing information for the user that changes over time. The images are generated if they are missing, and there is no requirement for them to exist for long periods of time. The images are served using image_tag.

The issue is that there are 2 types of intermittent problems that occur. One is that the image link generated will sometimes have the wrong fingerprint, even though the images will have just been updated before the image_tag is rendered. (Thus 404'ing.)

The second issue is that even though I can verify the image exists on the server, I still occasionally get a ActionView::Template::Error (The asset "gruff/image-1.png" is not present in the asset pipeline.):

If this should be working, how can I further dig into understanding what the issue is?

If the asset pipeline is simply not a good mechanism for what I'm trying to do, what would the community suggest instead?

1

There are 1 best solutions below

0
On

A better idea for your problem is save this generated image with activestorage, so you should create a model like that:

class DynamicImages < ApplicationRecord
  has_one_attached :image
end

And create a record when use gruff to generated this image.