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
