Chao Lam

Working on the next small things …

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(Time.now + 1.year)
    render :text=>'<script src="//connect.facebook.net/en_US/all.js"></script>' + "\n"
  end

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) -%>'});
Advertisements

Written by Chao

August 3, 2011 at 9:28 pm

Posted in Uncategorized

One Response

Subscribe to comments with RSS.

  1. thanks, saved me some time, while i was developing my facebook app.

    Sebastian

    November 26, 2011 at 5:10 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: