How to integrate Facebook Feed with a Rails app

On Monday, I’ve gotten Facebook Feed publishing to work with my site, and it took about two hours including TV watching time. I could’ve done it faster if I actually paid a full attention. It was all possible, thanks to Chris Schmitt, who has an excellent tutorial on his site.

1. Simply you first need to create a sub-class inherited from Facebooker::Rails::Publisher inside a controller. In my case, I wanted to publish a feed when a new playground is added and an existing playground is edited, so it made a sense to put it in playgrounds controller. publish_pg takes objects and sets parameters, and publish_pg_template creates a feed message based on those parameters.

class PlaygroundsController < ApplicationController

[SNIP - other actions]

  class FacebookPublisher < Facebooker::Rails::Publisher

    def publish_pg_template
      one_line_story_template "{*actor*} created/updated: {*pg_name*}"
      short_story_template "{*actor*} created/updated: {*pg_name*} in {*pg_city*}, {*pg_state*}",
                           "Check out what {*actor*} said, and rate or add comments to help other parents!"
    end

    def publish_pg(pg, facebook_session)
      send_as :user_action
      from facebook_session.user
      data :actor => facebook_session.user.first_name, :pg_name => pg.name, :pg_city => pg.city, :pg_state => pg.state, :pg_id => pg.id
    end

  end

end

2. Then you need to create database to store templates. You can follow Chris’ example exactly.

class CreateFacebookTemplates < ActiveRecord::Migration   def self.up     create_table :facebook_templates, :force => true do |t|
      t.string :template_name, :null => false
      t.string :content_hash, :null => false
      t.string :bundle_id, :null => true
    end
    add_index :facebook_templates, :template_name, :unique => true
  end

  def self.down
    remove_index :facebook_templates, :template_name
    drop_table :facebook_templates
  end
end

3. Finally you need to call the method, and you can do so by putting create in front, as Chris explains. In create action, I first check if logged-in user is a FB user, since if it’s called without existing facebook_session, it will barf. I have the similar check and call in update action for when a playground is edited.

def create
[SNIP]
  if @playground.save
    if @current_user.facebook_user?
      flash[:user_action_to_publish] = FacebookPublisher.create_publish_pg(@playground, session[:facebook_session])
    end
    redirect_to playground_path(@playground)
  else
    render :action => "new"
  end
end

Oh, and also, don’t forget to add the following between <header></header> tags in app/views/layouts/application.html.erb so that FB Feed window pops up after create or update action.

<%= fb_connect_javascript_tag %>
<% init_fb_connect "XFBML","Api" do %>
  <%= fb_user_action(flash[:user_action_to_publish]) if flash[:user_action_to_publish] %>
<% end %>

Happy integration!

One thought on “How to integrate Facebook Feed with a Rails app

Leave a Reply

Your email address will not be published. Required fields are marked *