How to insert the new Tweet button in your ruby on rails application

Twitter has just released their new Tweet button and I wanted to use it in the freelance marketplace I am developing (TaskArmy.com).

It took me literally 5 minutes thanks to the Twitter Button gem from Intridea.

PSD layout to WordPress

Although the gem worked perfectly locally, I had some issues when I deployed it on Heroku.

1. Install the gem:

gem install tweet-button

2. Add tweet-button to your .gems file

3. Add the following in your environment.rb:

config.gem "tweet-button"

4. Include the module in your ApplicationHelper:

module ApplicationHelper
  	include TweetButton

5. Call the button method in your view:

<%= tweet_button(:via => "taskarmy", :text => task.title, :count => "none") %>

6. That should be it for most of you. When I deployed to Heroku, I had an error saying that the method html_safe didn’t exist in the String class so I simply integrated the code tweet_button.rb of the gem in my project and I added a html_safe function in the class:

def html_safe(text)
       return text if text.nil?
       return text.html_safe if defined?(ActiveSupport::SafeBuffer)
       return text.html_safe! if text.respond_to?(:html_safe!)
       text
end

And change the two calls to html_safe in tweet_button.rb to the following:

''.html_safe –> html_safe('')

That’s it!

Other articles you might like

Tags: ,

Leave a comment