5

我正在构建一个在外部网站上进行转换跟踪的 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    

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

4

3 回答 3

8

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

来源:

http://forrst.com/posts/Render_a_1x1_Transparent_GIF_with_Rails-eV4

而不是渲染,调用

send_data(Base64.decode64("R0lGODlhAQABAPAAAAAAAAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="), :type => "image/gif", :disposition => "inline")
于 2013-12-16T21:00:29.910 回答
3

简单的选项:

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

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

于 2010-05-15T16:45:38.813 回答
1

这是我的解决方案

format.gif { send_data Rails.root.join('app', 'assets', 'images', '1x1.gif') }
于 2013-07-22T13:07:01.517 回答