ChangingThePresent
My Resume
Web DOC PDF
RTF ODT TXT
powered by emurse

facebooker plugin tutorial

bryan / 27.Jan.2008

this tutorial is currently only for rails, and assumes 2.0.2 or above

you'll need the plugin, so svn it, or, I use piston

piston import svn://rubyforge.org/var/svn/facebooker vendor/plugins/facebooker

There is a rake task for basic setup

rake facebooker:setup

that will create a facebooker.yml file, which you can then fill out with your facebook dev info. the authors also say to create the following:

	ActionController::Base.asset_host = "your address"
	ENV['FACEBOOKER_RELATIVE_URL_ROOT'] = '/canvas/'
  	ENV['FACEBOOK_API_KEY'] = 'api key'
  	ENV['FACEBOOK_SECRET_KEY'] = 'secret key'

which I placed in config/initializers/facebooker.rb

I then added 3 before filters to ApplicationController like this

	before_filter :ensure_application_is_installed_by_facebook_user
	before_filter :ensure_authenticated_to_facebook
	before_filter :get_user

	def get_user
		@facebook_user = facebook_session.user
    @user = User.find_or_create_by_facebook_uid(@facebook_user.uid)
	end

the first two are class methods of Facebooker::Session, and its a little redundant two call them as before filters the way I did, as the source code calls the same thing. It's convenient and matches my style to macro it at the top.
The last filter simply calls the function defined below, which sets the facebook user and the app's user.
You could model them as one, but I choose to keep them apart due to Facebook's privacy regs.

after that, you're pretty much back in familiar territory. the plugin has helper method for views, to call FBML through your view, like this, from my layouts/main.fbml.erb

	Hi <%= fb_name(@facebook_user) %>!

thats it for now, more soon

4 Comments

I think the part with config/initializers/facebooker.rb is not needed. Rdoc page must be out of date.
Hey thanks Nick. I'll check it out and post an update.
Hey Nick. I played around with removing/changing the values in facebooker.rb. As far as the security stuff goes, it seems definitely essential, as Facebook rejects the request without credentials. The other options tell facebooker where your callbacks are, as opposed to setting the same information in Facebook's app settings. Removing for example the relative url root environment variable, links generated for the FB side become 404's. My dev app is at "apps.facebook.com/rubyisbeautiful" If I remove that env from the config, my links _as far as Facebook is concerned_ become "apps.facebook.com" I checked again the rdocs for any updates, and they do seem dated. The last doc change was Nov 2007 and the last code change was Feb 6th. I'll regenerate the docs locally and post here for comparison. check the comments for the link. Thanks!
Hi Nick, One thing you might wanna do wich, in my view, would be cleaner is to def a current_user method like this in application controller. helper_method :facebook_session,:current_user def current_user @current_user ||= User.find_or_create_by_facebook_uid(facebook_session.user.uid) if facebook_session end this way, you can call current_user from anywhere you want and get it only when you need it. This is common practice for authentication in Rails. thanks for the blog post.

Sorry, comments are closed for this article.

back to top

micro theme by seaofclouds, and powered with Mephisto Hosting by hostingrails.com