In my free time I dabble in Ruby on Rails development and Facebook application development. Last year I picked up Mike Mangino’s book, Developing Facebook Platform Applications with Rails, which gives great step-by-step instructions for creating a facebook FBML application using the facebooker gem.
FBML is not the only type of Facebook application, however. There is also the iframe method, which Lead Facebook Engineer Charlie Cheever has been endorsing for a while. His blog post gives a good comparison of the two methods:http://www.ccheever.com/blog/?p=10 . In addition to this, the Facebook developer roadmap (http://developers.facebook.com/roadmap ) shows that soon you will not be able to create new FBML applications, and is recommending that existing FBML applications be migrated to iframe. The message is clear: time to switch to iframe people!
Unfortunately the tutorials performing this kind of switch are hard to come by. Mike Mangino has created a new RoR gem named facebooker2 to support iframe and facebook connect development but documentation is limited.
This blog post includes some documentation on how to convert a facebooker FBML app to a facebooker2 iframe app. Read Charlie Cheever’s blog post to understand the architectural differences between the FBML and iframe approach, especially the bit about server-side FBML. More details here: http://developers.facebook.com/docs/reference/fbml/serverFbml
Remove the facebooker gem
Get Facebooker2 and mogli
$gem install facebooker2
$gem install mogli
Reccomended: unpack gems to /vendor (http://errtheblog.com/posts/50-vendor-everything)
Update environment.rb, set
RAILS_GEM_VERSION = 2.3.5
Create an initializers/load_facebook_yaml.rb file to read from facebook.yml. It should include a single line:
Inside your facebook.yml file add the following two parameters to each environment config:
Rename application.rb to application_controller.rb. Add the following command to your application_controller.rb
Comment out the set_current_user method
Paste in this method:
@current_user ||= User.find(session[:user_id])
elsif current_facebook_user and @current_user.nil?
@current_user = User.find_by_facebook_id(current_facebook_user.id)
session[:user_id] = @current_user.id
Add a ‘ensure_authenticated_to_facebook’ method to keep making sure that the user has a facebook session when accessing your site. In my case my sessions/login page just displays “please login to facebook”, and since we’re in an iframe in the context of apps.facebook.com/myapp, there is a facebook login prompt displayed immediately above my page.
if current_user == nil
logger.info "current user is nil"
redirect_to :controller=>'sessions', :action=>'login'
With Facebook Connect it is not required that your application runs inside the context of apps.facebook.com/myapp. Your login page could technically detect if it is running in app.facebook.com and only display “please login”, otherwise if you are running it as a standalone page present the user with a facebook connect login button.
Enable oauth 2.0. This will facebook cause to send a “signed_request” request parameter to your application once the user has authenticated. Mogli will use this token to authenticate the user and create the current_facebook_user object.
Copy/Rename your .rhtml.erb files to .html.erb. You will no longer have a canvas and will never render rhtml files again.
Remove <%= facebook_messages %> in your erb files or replace them with <%= flash[:error] %> or <%= flash[:error] %>
<% fb_connect_async_js do %>
<%= yield :fb_connect%>
<% end %>
-facebooker2 reference project: https://github.com/mmangino/facebooker2_fb_connect_example
-images for stream publisher popups need to be accessible by facebook servers. You used to use a reverse tunnel so facebook could read them directly from your local server. If you have images that fall into this category you may still need to use a reverse tunnel.