How to Add Disqus Comments to a Rails 5 Application

19 December 2016 on . 3 minutes to read

Easy Comments

Even though we’re all capable of building a commenting system from scratch, sometimes public comments for an app are fine and even encouraged (such as enhancing visibility from other sites). I recently added comments to a client’s app and we decided on using Disqus, since it can be dropped in quickly, and all the commented on pages will be public. I’ve written this since the existing Disqus how to’s for Rails which showed up on a google search were outdated; Disqus has since changed their universal js snippet.

The Boilerplate JS

When you want to install Disqus, you register on their site, and they give you a code snippet to insert on every page which you want comments. For this example, we’ve got an Article model, and when viewing each article, want to display comments.

# app/views/articles/show.html.slim
# non relevant erb here 
= render 'disqus'

At which point we’d insert the vanilla Disqus JS into a partial:

<!-- app/views/articles/_disqus.html.erb !-->
<div id="disqus_thread"></div>
<script>
/**
*  RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
*  LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables
var disqus_config = function () {

};
*/
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = '//nimblefact.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>

From here, all that’s needed is modifying disqus_config as follows:

  this.shortname = "application_name";
  this.page.url = "<%= url_for(host: ENV['APPLICATION_HOST']) %>";
  this.page.identifier = 'article-<%= @article.id %>';
  this.page.title = '<%= @article.title %>';  

At which point, Disqus will load up comments for your page. Easy enough!

Disqus in Rails Application

The one customization I made to the recommended config is this.page.identifier. I scoped that to articles, rather than just inserting the article ID, so that in the future if other models needs comments they can be easily separated from the article comments.

Why Disqus Than Roll Your Own?

As I mentioned, there were no privacy concerns going into this (ie, all pages are public facing anyways), plus this will offload maintenance for both comment functionality and administration. This has enabled us to spend more time on items like addressing page load speeds, Elasticsearch enhancements and functionality which is unique to their business. It saves them money, and allows me to work on much more engaging projects, which is a win for everyone.


If you enjoy having free time and the peace of mind that a professional is on your side, then you’d love to have me work on your project.

Contact or view a list of available services to see how I’ll make your life better, easier and bring satisfaction back into you running your business.