4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ビーコン用1px透明GIF画像をRailsのコントローラから直接返す

Last updated at Posted at 2018-05-23

参考: https://stackoverflow.com/questions/2839521/returning-a-1x1-gif-as-a-response-in-rails

コントローラで以下のように実装

beacon_controller.rb
class BeaconController < ApplicationController
  def ping
    
    # セッションやcookieの処理などをココに書く
    
    send_data Base64.decode64("R0lGODlhAQABAPAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="),
              type: "image/gif",
              disposition: "inline"
    expires_now
  end
end

expires_nowを付け忘れると、2回目以降コントローラに処理が届く前にRailsが 304 Not Modifiedを返すので注意

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?