SyntaxHighlighter

Tuesday, April 14, 2009

Posting a Status Update to Facebook

OK - following up on my post about sending a status update to Twitter - here is a simple how-to with Facebook posts.

I really like the facebook system. It's got a lot more bells and whistles, which I usually don't like, but they went through some effort to let you pick a thumbnail to go along with the post and give you the option to send a message or post to your wall. Very nicely done. On the negative side, when a user clicks on the link from facebook, it opens in an iframe in a facebook URL. Not the worst experience - it's easy to close.

Anyway, without further ado, here's my code...

I link to this action via the link_to helper


<%= link_to image_tag('icon/facebook.gif'), {:controller => 'articles', :action => 'facebook', :id => @article}, :popup => ['new_window','width=600,height=400'], :rel => 'nofollow' %>



This is in the controller


def facebook_post
@article = Article.find(params[:id])
url = CGI.escape(url_for(@article, :only_path => false)
title = CGI.escape(@article.title)
redirect_to "http://www.facebook.com/sharer.php?u=#{url}&t=#{title}"
end

This will pop up a window that is 600x400 (works well with the facebook page that opens up).

No comments:

Post a Comment