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 示例