在 Rails 中返回 1x1 .gif 作为响应
我正在构建一个 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 图像。有更好的方法吗?
I'm building a Rails app that does conversion tracking on outside sites. I'd like to allow users to paste an image tag in their conversion pages (like AdWords), and whenever that image is requested, a conversion registers in my app.
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
This way, the browser gets a non-existent .gif image. Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这似乎是一个更简单的解决方案
来源:
而不是渲染,调用
This seems to be a simpler solution
Source:
instead of rendering, call
简单选项:
format.gif {
重定向到'/images/1x1.gif'
我认为
在 /really/ 旧浏览器(IE5,Netscape 也许?)中这可能不起作用,所以如果你需要支持这些,老式的解决方案是实际加载 gif 的二进制数据并将其吐出回浏览器直接使用正确的内容类型。
Simple option:
format.gif {
redirect_to '/images/1x1.gif'
}
I think in /really/ old browsers (IE5, Netscape maybe?) this may not work, so if you need to support those, the old school solution was to actually load in the binary data of the gif and spit it out back to the browser directly with the correct content type.
这是我的解决方案
Here is my solution