Rails tip of the Day (#2, new style for Facebook user invites)

Posted on November 26, 2007

ok, so i know i said i was going to continue with routes with my next tip post, but to be honest, I almost fell asleep just trying to plan the post out. Needless to say, my heart really wasn’t in it. So instead, here’s a facebook developers tip!

Recently-ish Facebook made it loads easier for app developers to add “invite your friends!” functionality into your app and since they don’t have any examples of how to use it in Rails, here you go.

First with the action in the controller, I grab all of the friends of the current user and pull out the ones who already have the app installed

    def invite
    fql =  "SELECT uid, name FROM user WHERE uid IN" +
        "(SELECT uid2 FROM friend WHERE uid1 = #{@current_user.fb_user_id}) " +
        "AND has_added_app = 1" 
    xml_friends = fbsession.fql_query :query => fql
    @friends = Hash.new
    xml_friends.search("//user").map do|usrNode| 
      @friends[(usrNode/"uid").inner_html] = (usrNode/"name").inner_html
    end
    @friend_ids = []
    @friends.each do |uid, name|
      @friend_ids << uid
    end
    @friend_ids = @friend_ids.join(',')
  end

now for the view, here is the fbml I stuck in my canvas page (notice the using of the @friend_ids in the exclude_ids paramter, sneaky sneaky)...

    <fb:fbml> 
    <fb:request-form action="<%= facebook_send_invites_path %>" method="POST" invite="true" type="social gamer" 
                    content="social gamer is the best app for gamers who actually game! 
                    once you join, you can rate games, review games and even loan out your games to 
                    your friends. <fb:req-choice url='http://www.facebook.com/add.php?api_key=724faa99b85cde63ed19088ca917e482' label='check out social gamer...now' />"> 
    <fb:multi-friend-selector showborder="false" actiontext="invite your friends to use Social Gamer...now" exclude_ids="<%= @friend_ids %>"> 
    </fb:request-form> 
  </fb:fbml>

yeah, hawt innit? now sure the controller code can be refactored (like only parsing out the uids probably instead of the actual users), but this is what I was able to just convert from the old code I had. If I think the refactor is good enough after I dig back into the xml I have to parse I’ll post it but this should be good enough to get you started. Also note that I’m using rFacebook here which I believe now depends on hpricot so you’ll need at least those two gems to do this.

You can dig more into the fb:request-form tag on the facebook fbml wiki page

jQuery + Rails = jRails

Posted on November 22, 2007

We all knew it was only a matter of time before someone created a jQuery replacement for Prototype and here it is, jQueryjRails. It’s a 0.1 release so it may not cover all edge cases well, but it does provide support for all of the functionality found in the built in Rails prototype_helper and some of the built in scriptaculous helpers.

jQuery for Rails via jRails

Rails tip of the day (#1)

Posted on November 22, 2007

Tip #1 – Know your routes

Knowing how your routes work in your Rails project is crucial. Knowing how what routes to where or how to force requests for one thing into a certain direction are all key to bending your app to your will. Here’s what I think are the most important route shenanigans to know about:

Using the command line to cheat Two shortcuts you need to know about here. The first one takes place in script/console. After you pop into script/console, type

 app.

followed by the beginning of what you think you’re route may be, like the object’s class or something. For example,

app.book

Hit tab twice and you’ll be shown all of the routes generated for that object.

rake routes The second tip is the “rake routes” rake task. Type and run “rake routes” from your project root and marvel at the list of routes it cooks up.

Tomorrow I’ll go over a few more route tips, promise.

Open social is now live!

Posted on November 02, 2007
Time to dig into and see if this OpenSocial is all Google and everyone but Facebook is hyping it up to be. So far, it looks like the only site live with the integration is Orkut and you need an Orkut account to jam although, I think your existing Gmail account will have you covered.

So go create one and immerse yourself in reading with me.

OpenSocial front page

Api Docs

Orkut Sandbox