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:
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!
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.