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

A fix for this rFacebook bug

Posted on October 21, 2007

So I updated to the latest rFacebook gem and unpacked it in one of my Rails powered Facebook apps and lo and behold, things started breaking in my main app. See, I have this main app (which I’ll reveal soon) which has a Facebook side to it. Well, installing the latest gem wreaked havoc on a few named routes.

I was getting this odd error…

wrong number of arguments (2 for 1) : 
in /rfacebook-0.9.7/lib/rfacebook_on_rails/controller_extensions.rb:463:in `url_for__ALIASED'

from this route…

 redirect_to users_path

Odd eh? So line 463 in the controller_extensions file looks like this…

path = url_for__ALIASED(options, *parameters)

Looks ok I guess but when I checked out what the options and parameters were, changing the above link to this…

path = options

Fixed the error for me. I don’t think this should have any adverse effect anywhere else, but if I’m wrong, feel free to enlighten me. :D