Search code examples
ruby-on-railsrubyruby-on-rails-4iframetwitter

Tweet button disappears on navigating through link rails 4 twitter


I have added a tweet button in my app that appears when I land on a page initially or refresh the page but doesn't display the button when I navigate through a link. It only displays the text written within a tag. I can see the difference in the two cases through inspect element.

<a href="https://twitter.com/share" class="twitter-share-button" data-url="http://example.com" data-via="sometext" data-size="large">Tweet</a>
<script>!function (d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0], p = /^http:/.test(d.location) ? 'http' : 'https';
    if (!d.getElementById(id)) {
    js = d.createElement(s);
    js.id = id;
    js.src = p + '://platform.twitter.com/widgets.js';
    fjs.parentNode.insertBefore(js, fjs);
  }
}(document, 'script', 'twitter-wjs');</script>

I have copied the above code from this link https://dev.twitter.com/web/tweet-button

The difference in the two cases:

Case 1: When the button appears:

<iframe id="twitter-widget-0" scrolling="no" frameborder="0" allowtransparency="true" src="http://platform.twitter.com/widgets/tweet_button.7d9dd43d4f18b1bb51cc9d8f0997995e.en.html#_=1427878870272&amp;count=horizontal&amp;dnt=false&amp;id=twitter-widget-0&amp;lang=en&amp;original_referer=http%3A%2F%2Flocalhost%3A3000%2Fusers%2Fedit&amp;size=l&amp;text=Draft%20Picks&amp;url=http%3A%2F%2Fexample.com&amp;via=sometext" class="twitter-share-button twitter-tweet-button twitter-share-button twitter-count-horizontal" title="Twitter Tweet Button" data-twttr-rendered="true" style="width: 140px; height: 28px;"></iframe>
<script>!function (d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0], p = /^http:/.test(d.location) ? 'http' : 'https';
    if (!d.getElementById(id)) {
    js = d.createElement(s);
    js.id = id;
    js.src = p + '://platform.twitter.com/widgets.js';
    fjs.parentNode.insertBefore(js, fjs);
  }
}(document, 'script', 'twitter-wjs');</script>

Case 2: When the button disappears

<a href="https://twitter.com/share" class="twitter-share-button" data-url="http://example.com" data-via="sometext" data-size="large">Tweet</a>
<script>!function (d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0], p = /^http:/.test(d.location) ? 'http' : 'https';
  if (!d.getElementById(id)) {
    js = d.createElement(s);
    js.id = id;
    js.src = p + '://platform.twitter.com/widgets.js';
    fjs.parentNode.insertBefore(js, fjs);
  }
}(document, 'script', 'twitter-wjs');</script>

I will be really thankful if someone figure out how is iframe displayed or removed over here or a proper solution to this. A very many thanks in advance!!


Solution

  • It was basically a turbolink issue. My javascript was not loaded. Those links on which I did want javascript to work I just added this line of code after link_to tag

    data: {no_turbolink: true}