facebook开放图表抓取工具在rails操作中触发json响应 [英] facebook open graph crawler triggering json response in rails actions

查看:163
本文介绍了facebook开放图表抓取工具在rails操作中触发json响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,Facebook搜寻器正在触发我的rails操作中的json响应。这导致操作只返回对象的json表示,而没有正常的html标记+打开图形标签。我已经用rails 3.2.6测试了。我使用facebook开放图表调试器来查看刮刀的显示内容: http://developers.facebook.com/tools/debug

For some reason the facebook crawler is triggering the json response in my rails actions. This causes the action to just return a json representation of the object, without the normal html markup + open graph tags. I have tested this with rails 3.2.6. I use the facebook open graph debugger to see what the scraper is seeing: http://developers.facebook.com/tools/debug.

代码非常简单。想象一下一个对象的简单show动作,例如User。结束于:

The code is very simple. Imagine a simple "show" action for an object, for example a User. It ends with:

respond_to do |format|
  format.js { render :json => @this.to_json }
  format.html
end

Facebook搜寻器是触发format.js,这会导致打开的图形标签不被渲染。任何想法为什么会发生或如何解决它?谢谢。

The facebook crawler is triggering the format.js, which causes the open graph tags to not be rendered. Any ideas why this might happen or how to fix it? Thanks.

推荐答案

好吧,Facebook发送一个接受标题

Ok so Facebook sends an accepts header of

*/*

由于没有请求特定的格式,按顺序排列respond_to块。如果您在以下的respond_to块中列出您的js,则rails将使用JSON响应Facebook打开的爬网程序,该操作将无法正常工作:

Since no specific format is requested, rails just goes down the respond_to block in order. If you list your js first in the respond_to block like below rails will respond to the facebook open crawler with JSON which will not work:

respond_to do |format|
  format.js { render :json => @this.to_json }
  format.html
end

只需切换订单所以默认情况下,rails会用HTML响应:

Just switch the order so by default rails responds with HTML:

respond_to do |format|
  format.html
  format.js { render :json => @this.to_json }
end

我不知道为什么Facebook没有指定他们正在寻找的格式...对我来说似乎很愚蠢。希望这有助于有人在路上。

I'm not sure why Facebook does not specify the format they are looking for... seems pretty idiotic to me. Hopefully this is helpful to somebody down the road.

这篇关于facebook开放图表抓取工具在rails操作中触发json响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆