Back to Blog

Get in Touch

New Twitter Button Gem from Intridea

By Jonathan Nelson August 12, 2010 in open source, twitter, gem

Missing

This week Twitter launched the official “Tweet Button,” a button for website owners to count RT’s and let readers easily share content. Mashable was first to report on this shiny new button, but we’re the first to release a tweet-button gem for your next project.

Introducing tweet-button

A new Ruby on Rails gem/plugin to generate shiny new Twitter buttons.

image provided via Mashable.com

Usage

First, include the TweetButton module into your application helper. After that, using it is as simple as adding a single method call to your views:

<%= tweet_button %>

Bam. Done. You’ll have a sweet lookin’ Tweet button all up in your view.

Of course, you can customize it. The method takes a few options. Any default can be overridden universally.

:url - The URL to share; the default is the current URL.
:text - The text that will appear in the tweet; the default is "Check this out!"
:via - The attribution. Defaults to "tweetbutton", but you should change that.
:lang - Set the language for the tweet (no default).
:related - Related Twitter accounts (no default).
:count - The tweet count box position (values can be "none", "horizontal", or "vertical"; default is "vertical").

So, if you wanted to tweet about Hacker News, attribute it to Peter Cooper, and add some custom text, all from a tweet button with a horizontal counter, you’d do this:

<%= tweet_button(:via => "peterc", :url => "http://news.ycombinator.com", :text => "AWESOME.")

Simple enough, eh? Also, this method call will include the Twitter JavaScript into the page (it only does it once, even if you have multiple buttons on the page). To put this wherever you’d like (i.e., your header), then use the twitter_widgets_js_tag method. If you call this method, it will place the tag wherever you call it from (and only place it there; subsequent calls do nothing).

The gem also supports the custom Twitter share links. To generate one, use the custom_tweet_button (aliased to custom_tweet_link also) method:

<%= custom_tweet_button %>

This will generate a link that will link to the share page with the same default options as the standard Tweet Button generator. You can customize your custom link with text as the first argument, the same options as tweet_button (with the exception of the count parameter, which will be ignored) as the second, and HTML options as a third argument. For example:

<%= custom_tweet_button('Tweet it!', {:via => "myself"}, {:class => "tweet-sharey-thing"})

Setting universal defaults

You can set a new default for any option by setting default_tweet_button_options in your application helper. For example:

module ApplicationHelper
  include TweetButton

  TweetButton.default_tweet_button_options = {:via => "myself"}
end

Only the options you specify will be overridden; so if you only specify a new default +:via+ (which you should definitely do), then the other defaults will stay intact.

Coming Soon

Tweet Buttons can also live in an iframe, so we’ll probably be adding that very soon!

Plan on using this gem?

If you plan on using this gem, please let us know in the comments section! We’d love to check it out :-)

Roll the credits!

This awesome sauce gem was written by our very own hackstar Jeremy McAnally. You can follow his tweets here and his Github here.

Medium

Jonathan Nelson

Jonathan is responsible for managing ideas & innovation, assessing current and new markets, creating strategies, defining business requirements, creating unified product plans, use cases/user stories and orchestrating product and design development. Jonathan is a results-oriented marketing and technology professional with over 9 years of professional experience in launching new products and turning emerging business opportunities into profitable, mature businesses. His past experience includes a proven track record in multichannel marketing, marketing strategy and revenue generation for eCommerce Web sites in over 10 Fortune 500 companies, and marketing expertise across multiple industries.

More posts by Jonathan Nelson

Jonathan Nelson

Today "Facebook announced":http://blog.facebook.com/blog.php?post=41817520213...

Jonathan Nelson

This week Twitter launched the official "Tweet Button," a button for website ...

Jonathan Nelson

Today, Intridea.com got a brand new look and feel. The goal behind the redes...