Facebook Open Graph Action + Rails: Returns JSON or HTML

I implemented a Facebook Open Graph for every great Ryan Bates tutorial: http://railscasts.com/episodes/363-facebook-open-graph

However, when Facebook scrapes my site to get og options, it looks like it is requesting the JSON format. This is a problem since I am already using JSON to return data for other purposes. I normally return data for Facebook via format.html . I checked the request object and I see this ACCEPT header:

 'HTTP_ACCEPT' */* 

However, this causes my application to execute format.json . I played with the order of the responses in the format, and it still asks for format.json .

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

Any ideas?

+1
source share
2 answers

After a lot of trial and error, I realized that with Accept Header */* , and if you use respond_with , you need to make sure that respond_to at the top of the controller matches your order ... In my case, this is correct:

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

and it is not

 class ProjectsController < ApplicationController respond_to :json respond_to :html respond_to :js, ... 
+4
source

Thank you so much!

Note: you may also need to reorder:

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

in

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

as facebook open graph crawler says , triggering json response in rails actions

0
source

All Articles