Spending my days programming virtual worlds for The Electric Sheep Company. Spending nights and weekends on BlitzPick for Super + Fun. I also enjoy quilting, reading, and singing in the shower.

Overriding link_to in a Ruby on Rails/Facebooker iframe app

I’m working on a Facebook app using Ruby on Rails and Facebooker. The app works as a Connect site or in an iframe app. Navigation in iframe apps is trickier than in fbml apps because links with the default target change only the iframe address, not the page address (shown in the address bar). Furthermore, Facebook only adds the fb_sig_ params when the Facebook app page is loaded. When a user clicks a link that changes only the iframe url, the fb_sig_ params aren’t passed along automatically.

After reading this blog entry and its comments, I decided to get around the issue by overriding link_to to automatically set the target to _top for links in the iframe app. I’m using the absolute url for those links, which feels wrong but I’m not sure how else to make sure it sets the address to the Facebook page instead of the canvas url.

link_to in ApplicationHelper:

def link_to(name, options = {}, html_options = {})
  if params[:fb_sig_in_iframe]
    rel = url_for options
    url = "http://apps.facebook.com/" + FACEBOOKER['canvas_page_name'] + rel
    html_options[:href] = url
    html_options[:target] = "_top"
    super(name, {}, html_options)
  else 
    super(name, options, html_options)
  end
end

Maybe later I’ll handle passing the fb_sig_ params instead of changing the page address. Setting top.location.hash would still allow for direct links (with additional work) and would load faster.

Monday, November 30th, 2009

blog comments powered by Disqus