Facebook Open Graph Action + Rails:返回JSON或HTML [英] Facebook Open Graph Action + Rails: return JSON or HTML

查看:166
本文介绍了Facebook Open Graph Action + Rails:返回JSON或HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照Ryan Bates的令人敬畏的教程实施了Facebook的Open Graph: http:// railsscasts.com/episodes/363-facebook-opengraphs

I've implemented Facebook's Open Graph per Ryan Bates' awesome tutorial: http://railscasts.com/episodes/363-facebook-open-graph

但是,当Facebook正在抓取我的网站以检索 og 参数,它看起来像是要求JSON格式。这是一个问题,因为我已经在使用JSON返回数据用于其他目的。我可以通过 format.html 返回Facebook的数据。我检查了请求对象,我看到这个ACCEPT标头:

However, when Facebook is scraping my site to retrieve the og parameters, it looks like it's asking for JSON format. That's a problem since I'm already using JSON to return data for other purposes. I'm ok returning data for Facebook via format.html. I checked the request object and I see this ACCEPT header:

'HTTP_ACCEPT' */*

但是,这导致我的应用程序执行 format.json 。我播放了格式响应的顺序,它仍然要求 format.json

However, that's causing my app to execute format.json. I played around with the order of the format responses and it still requests format.json.

respond_with(@project) do |format|
    format.html { render 'show'}
    format.json { render 'show'}
    format.js { render 'show'}
end

任何想法?

推荐答案

经过大量的试用和错误,我发现使用 * / * 的接受标头,如果您使用 respond_with ,您需要确保控制台顶部的 respond_to 符合所需的顺序...在我的情况下,这是正确的: / p>

After a lot of trial and error, I figured out that with an Accept Header of */*, and if you are using respond_with, you'll need to make sure the respond_to at the top of the controller matches the order you want... In my case, this is correct:

class ProjectsController < ApplicationController
  respond_to :html
  respond_to :json
  respond_to :js,
  ...

这不是

class ProjectsController < ApplicationController
  respond_to :json
  respond_to :html
  respond_to :js,
  ...

这篇关于Facebook Open Graph Action + Rails:返回JSON或HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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