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

Continue reading