ruby-on-railsajaxruby-on-rails-3ruby-on-rails-3.2disqus

Disqus Ajax is not taking different thread for diffrent posts in ruby on rails


I have one rails application where I am using Disqus Universal Code for comment function. I am using ajax remote => true function to display my posts content. Here is my disqus code

 <div id="disqus_thread"></div>
   <script type="text/javascript">
      var disqus_shortname = 'ginfy';
      var disqus_identifier = '<%= @posts.id %>';
      (function() {
        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
  </script>
  <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript> 

I know that Disqus depends on Site URL and it takes different thread based on different URL but here i am using AJAX [No CHANGE IN URL]. check my erb-

<%= link_to "Read more","prayers/#{posts_item.id}",:remote => "true" %>

When I click on read more then Disqus always open with same Thread. I want Different disqus thread for different posts without sacrificing my Ajax functionality.

I already checked all the question related to this problem but nothing gave me solution. Please help me.


Solution

  • You'll need to use the Disqus.reset function when you want to change the thread. Something like the following would work:

    <script type="text/javascript">
        var changeThread = function(){
    
            DISQUS.reset({
              reload: true,
              config: function () {  
                this.page.identifier = 'new_disqus_identifier';
                this.page.url = 'http://example.com/#!new_content';
                this.page.title = "New Page Title";
                this.language = "fr"; // only if changing the language
              }
            });
    
        };
    </script>
    
    <button onclick=changeThread();>Change the Disqus thread!</button>
    
    <div id="disqus_thread"></div>
        <script type="text/javascript">
            var disqus_shortname = 'ginfy';
            var disqus_identifier = '<%= @posts.id %>';
            (function() {
                var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
                dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
                (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
            })();
        </script>
    <noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>