Assumptions: You have a rails application & you want to create a Plugin on Facebook for it. You want it integrated into Facebook & to have the look and feel of Facebook.
Disclaimer: I am writing this as I learn it, it could be all wrong, I will update as I learn more, send me corrections and I will fix.
Outcome : After doing this, you will be able to run your rails app in stand alone mode and as a facebook fbml canvas on the same rails tree. You will have a base fbml tab set in a layout along with a way to add actions & views.
1.) Start by going here and following the directions (up to step 8)
Couple specific things for your app:
- chose fbml & website
- callback url = http://your-public-ip:your-public-port
- make a unique name for your app, remember it, its used for your canvas path below.
2.) Install Facebooker:
script/plugin install http://facebooker.rubyforge.org/svn/trunk/facebooker/
(facebooker is changing rapidly, always make sure you have the latest version of this,
even if you downloaded last week, its tool old)
3.) Configure Facebooker: (if you installed via plugin, it should have created a file
called facebooker.yml in your config directory. Edit this file and add your keys,
also use your canvas name for canvas_page_name. Also add your callback_url it should be the same as the facebook setting (your ip and port)
* your api key & secret key can be found on the my applications page of the facebook
app you create.
4.) Create a new controller called FaceController (you can use script/generate)
5.) Add to top of your new controller:
ensure_application_is_installed_by_facebook_user
ensure_authenticated_to_facebook
6.) Add this line to the top of your routes.rb file
map.facebook_root '', :controller=>"face", :conditions=>{:canvas=>true}
7.) Add this line to your environment file (development.rb)
ActionController::Base.asset_host = "http://myip:myport"
(this will eventually move the the facebooker.yml file, so if you see it there,
dont do this step)
8.) Create an index method on your new controller
#controller
def index
@userF = session[:facebook_session].user
end
9.) Create a layout for Facebook apps (in your layouts folder, face.rhtml)
<fb:dashboard>
<fb:header decoration="add_border" icon="false">MyApp</fb:header>
<fb:create-button
href="<%= facebook_root_url + url_for(:controller => 'face', :action => "new", :only_path => true,:skip_relative_url_root => true) %>">
New Thing</fb:create-button>
</fb:dashboard>
<fb:tabs>
<%= fb_tab_item("My Stuff", facebook_root_url + url_for(:controller => :face,:action => :my_stuff,
:only_path => true, :skip_relative_url_root => true)) %>
<%= fb_tab_item("Other Tab", facebook_root_url + url_for(:controller => :face,:action => :other_tab,
:only_path => true, :skip_relative_url_root => true)) %>
<%= fb_tab_item("Settings", facebook_root_url + url_for(:controller => :face,:action => :my_settings,
:only_path => true, :skip_relative_url_root => true)) %>
</fb:tabs>
<%unless flash[:notice].blank? %>
<fb:status><%= flash[:notice] %></fb:status>
<%end%>
<%= yield %>
10.) Create a view (index.rhtml in face view folder)
Hello from facebooker <BR><br/>
My ID: <%=@userF%> <BR/>
<%=fb_profile_pic(@userF) %>
Friends: <BR/>
<% for a_friend in @userF.friends %>
<%= fb_name a_friend %><BR>
<% end %>
11.) Try it out:
http://apps.facebook.com/myapp - coming in this way all requests get routed to your FaceController.
or still use your local rails app
http://myappip:myappport - here you should see the regular rails application.
Thanks to the Facebooker team for writing this awesome application and sharing with us and having the
patience to answer our questions! (I asked about 20 questions to get to this point)
Trying out your sample code, I always get the following exception: #
It seems that I need an "infinite session key". Do you know what I did wrong?
Posted by: Matt | June 07, 2008 at 07:47 AM
the exception: Facebooker::Session::MissingOrInvalidParameter: Invalid parameter
Posted by: Matt | June 07, 2008 at 09:19 AM
Hi,
For persons having various exceptions for sessions, have you correclty said Rails to use ActiveRecord for storing sessions?
Posted by: aurels | June 18, 2008 at 07:38 PM
The link in step 1 is broken, do you have an alternate link?
Posted by: Tom | August 12, 2008 at 12:02 PM
I need to generate links in for my application. I use url_for(:controller => 'posts' , :action => 'index').But the links are broken, like it generates /posts/index instead of http://appa.facebook.com/myapp/posts/index.I don't use a named route.Will url_for work in facebooker only if i pass the :canvas,:only_path and :skip_relative_path params?
Posted by: prabha | August 15, 2008 at 01:24 PM
Thanks for tutorial. I have installed Facebooker plug-in about to create controller as suggested in tutorial and got following error.
Did I miss anything ?
===============================================================
C:\temp\quickstart>ruby script\generate controller Face index
c:/ruby186/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original
_require': C:/temp/quickstart/vendor/plugins/facebooker/lib/facebooker/models/us
er.rb:288: syntax error, unexpected $end, expecting kEND (SyntaxError)
from c:/ruby186/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
from c:/ruby186/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_su
pport/dependencies.rb:510:in `require'
from c:/ruby186/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_su
pport/dependencies.rb:355:in `new_constants_in'
from c:/ruby186/lib/ruby/gems/1.8/gems/activesupport-2.1.2/lib/active_su
========================================================
Thanks in advance
Regards
Rajan
Posted by: Rajan | January 15, 2009 at 05:37 PM
Hey Rajan, this example is really old, I wouldn't use it anymore. But I think your problem is from facebooker, see : http://railsforum.com/viewtopic.php?pid=84680. You may just want to add the end for now and go on.
Posted by: Joel Nylund | January 15, 2009 at 07:24 PM