Rails - How to send an image from a controller

前端 未结 2 1533
眼角桃花
眼角桃花 2020-12-24 08:06

in my rails app, I need to pass back a image.

I have a 1x1.gif tracking pixel in my route as follows:

 match \"/_ctrack.gif\" => \"email_tracking_         


        
2条回答
  •  一整个雨季
    2020-12-24 08:43

    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.

提交回复
热议问题