Rails - How to send an image from a controller Rails - How to send an image from a controller ruby-on-rails ruby-on-rails

Rails - How to send an image from a controller


Wouldn't be better to use send_file instead?

send_file Rails.root.join("public", "file.gif"), type: "image/gif", disposition: "inline"


Is there a reason that you cannot save the file to public/_ctrack.gif, remove the route, and let the underlying web server serve the image?

If you need to process the image from disk, just use open on the local filename:

send_data open("#{Rails.root}/path/to/file.gif", "rb") { |f| f.read } .......

The rb sets the file to open and binary modes.