Chao Lam

Working on the next small things …

Archive for August 2011

Optimizie your Facebook Social Plugin with Ruby On Rails

with one comment

Facebook is now officially suggesting you add a “channel url” in the FB.init() call for their Javascript SDK.

Here’s how to do it in Ruby on Rails. Add code like below in a controller

  def fb_channel
    response.headers['Pragma'] = 'public'
    expires_in 1.year, :public=>true
    response.headers["Expires"] = CGI.rfc1123_date( + 1.year)
    render :text=>'<script src="//"></script>' + "\n"

I added this in a MiscController that I usually have hanging around for non RESTful paths like this.
In Rails 3, I would add this line in my routes.rb:

  match '/fb_channel', :controller=>'misc', :action=>'fb_channel'

and when I do my FB.init() call, I’d do something like this:

    FB.init({appId:appId, status: true, cookie: true, xfbml:true,
      channelUrl: '<%= url_for(:controller=>'misc', :action=>'fb_channel', : only_path=>false) -%>'});

Written by Chao

August 3, 2011 at 9:28 pm

Posted in Uncategorized