在Rails中返回1x1 .gif作为响应

Avi*_*hai 4 tracking ruby-on-rails google-adwords

我正在构建一个Rails应用程序,可以在外部站点上进行转换跟踪.我想允许用户在其转化页面(例如AdWords)中粘贴图片代码,并且无论何时请求该图片,我的应用都会转换注册.

respond_to do |format|
  if @conversion.save
    flash[:notice] = 'Conversion was successfully created.'
    format.html { redirect_to(@conversion) }
    format.xml  { render :xml => @conversion, :status => :created, :location => @conversion }
    format.js { render :json => @conversion, :status => :created }
    format.gif { head :status => :ok }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @conversion.errors, :status => :unprocessable_entity }
  end
end    
Run Code Online (Sandbox Code Playgroud)

这样,浏览器就会获得一个不存在的.gif图像.有一个更好的方法吗?

Kev*_*vin 6

这似乎是一个更简单的解决方案

资源:

http://forrst.com/posts/Render_a_1x1_Transparent_GIF_with_Rails-eV4
Run Code Online (Sandbox Code Playgroud)

而不是渲染,调用

send_data(Base64.decode64("R0lGODlhAQABAPAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="), :type => "image/gif", :disposition => "inline")
Run Code Online (Sandbox Code Playgroud)


Gal*_*Gal 3

简单选项:

format.gif {redirect_to '/images/1x1.gif' }

我认为在/真正/旧的浏览器(IE5,Netscape 也许?)中这可能不起作用,所以如果你需要支持这些,老式的解决方案是实际加载 gif 的二进制数据并将其吐回到浏览器直接使用正确的内容类型。