misschoksondik
misschoksondik

Reputation: 591

facebook comments (Facebook social plugin) load only after I refresh the page in rails 4 app

I have a rails 4 app and I want to use the Facebook social plugin for comments. I have this code: in show.html.erb I have

<div class="fb-comments" data-href="my site name" data-numposts="5" data-colorscheme="light"></div>

in application.js:

( function(d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id))
            return;
        js = d.createElement(s);
        js.id = id;
        js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=644565478915600";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));

And after my opening body tag:

<div id="fb-root"></div>

When I go to the page, the comments appear only after I refresh it. Why?

Upvotes: 4

Views: 1150

Answers (3)

Lauro Silva
Lauro Silva

Reputation: 1

Yes. I'm building a blog and on the Post I had the same problem. I did not want to disable Turbolinks. So the solution was to add the following to my show.html.erb:

<% if current_page?(YOUR PATH) %> <body data-no-turbolink="true"> <% else %> <body> <% end %>

On YOU PATH you have to add the following page where you want to disable Turbolink.

Worked like a charm!

Upvotes: 0

There's an official solution to this known problem. wich it seems to be generated by the use of turbolinks. It's described here. What I did was to add the provided coffescript to my page related coffee file (events.coffee in my case, placed in app/assets/javascript) and it worked like a charm. There's even no need of the <div id="fb-root"></div> tag. Hope it helps!

Upvotes: 0

Zippo9
Zippo9

Reputation: 706

Remover Turbolinks. Also you can install jquery Turbolinks gem and then in your application.js file add //require jquery Turbolinks and remove // require Turbolinks

Upvotes: 1

Related Questions