emptygif
語言: 繁體中文

所有語言和框架

Ruby on Rails Ruby

適用於 Ruby on Rails 的 Ruby 程式碼片段,以空 GIF 回應對 /pixel.gif 的請求,GIF 在啟動時只解碼一次。

Ruby on Rails Ruby

安裝
gem install rails && rails new app
檔案
pixels_controller.rb
執行
bin/rails server -p 8080
# app/controllers/pixels_controller.rb
# ActionController::API: no session, cookies or browser-version checks for a beacon.
class PixelsController < ActionController::API
  GIF = "R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==".unpack1("m0").freeze

  def show
    no_store
    send_data GIF, type: "image/gif", disposition: "inline"
  end
end

# config/routes.rb
Rails.application.routes.draw do
  get "/pixel.gif", to: "pixels#show", format: false
end

我們執行了這段程式碼,並比對了它回傳的位元組。

驗證

curl -sI http://localhost:8080/pixel.gif

官方網站: Ruby on Rails

更多 Ruby 範例