02 December 2009
Filed under Blog

Add a retweet button to your Rails app easily with the Backtweets helper

On this blog, I use the excellent Socialize plugin to easily add social sharing buttons to each post. For Twitter, I chose to configure Socialize to use Backtweets for displaying tweet counts mainly because it's more configurable than the TweetMeme widget.

When I needed a Backtweets widget for a client Rails application, I whipped up a quick plugin that provides an easy helper for writing all those JavaScript options.

Installation

You can install the plugin from the GitHub repo:

script/plugin install git://github.com/pengwynn/backtweets_helper.git

Displaying a tweet count in your view

To display a count of tweets that reference the current page, simply call the helper:

<%= backtweets_count %>
<%= backtweets_count(:size => :small) %>

The full set of options include:

Parameter Default value Description
url Current URL The URL to measure tweets for
title Title of the current page The title of the page for retweets
src Leading text for retweets
via true If tweets contain '(via @BackType)'
links false If links should be opened in new windows
size large Size of button (large or small)
api_key Awe.sm API Key for custom URL shortener
short_url Generated (bt.io) Specify the pre-shortened URL that you would like to use
background Button background color (e.g. FFFFFF)
border Button border color (e.g. CCCCCC)
text Button text color (e.g. 000000)

Displaying recent comments, blogroll and search

The plugin also includes three other helpers to render user comments and blogroll widgets as well as a comment search widget:

<%= backtweets_comments('pengwynn') -%>
<%= backtweets_blogroll('pengwynn') -%>
<%= backtweets_search('ruby') -%>

If you would like to hit the Backtweets API for more info on your links and counts, check out the Twitterland gem.

Like this? Why not grab the feed?