Twitter Share and Follow Buttons in Rails 5 App

Tweet Button to Share Content

You can implement and go live in just 5 minutes. In the view, add the Tweet button code shown below:

 <div class="social">
      <a href="http://twitter.com/share" class="twitter-share-button" data-url="<%= episode_url(@episode) %>" data-text="Watching <%= @episode.title %> on RubyPlus" data-count="horizontal" data-via="ruby_plus">Tweet</a><script type="text/javascript" src="https://platform.twitter.com/widgets.js"></script>
    </div>

The data-via value should be the name of your Twitter account. The data-url is the show page for article or episode in my case. The data-text value can be any text. In this case, it displays:

Watching Securing an API using Token Based Authentication in Rails 5 on RubyPlus followed by the link to the episode and the Twitter account handle. Watch it in action, by clicking this link: Securing an API in Rails 5.

Twitter Follow Button

You need to provide the class twitter-follow-button for Twitter javascript to recognize the html. Replace @ruby_plus with your Twitter handle name and the link to your Twitter account in the href tag.

<a href="https://twitter.com/ruby_plus" class="twitter-follow-button">Follow @ruby_plus</a><script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

You can see this in action in the about page.


Related Articles


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.