William Notowidagdo Kiranatama Staff
Knowledge


I will show you how to use Juvia as the commenting system for your Rails application.

What is Juvia?

Similar to Disqus and IntenseDebate, Juvia is a commenting system. You can put your commenting needs to Juvia so that you don't have to build your own. Embedding Juvia on your web page is easy, you just need to paste a JavaScript snippet.

Juvia installation

Head over to github.com/phusion/juvia and follow the steps in "Installation" section. However I found my self doing these stuff in order to finish the installation steps provided.

On step #3, use mysql2 instead of mysql as adapter

production:
   adapter: mysql2
   database: juvia
   host: localhost
   username:

Before step #4, add gem 'therubyracer' into group :assets

group :assets do
   gem 'sass-rails',   '~> 3.1.5'
   gem 'coffee-rails', '~> 3.1.1'
   gem 'therubyracer'
   gem 'uglifier', '>= 1.0.3'
 end

Before step #5,

$ cp config/application.yml.example config/application.yml

and change the configuration as you need.

Throw Juvia to Passenger or whatever application server you prefer. When you access Juvia for the first time, it will ask you to create an initial administrator account to register a site.

After your site has been registered, Juvia will show a snippet of code for you to embed into your web pages.

Blog application

For the purpose of this post, I created a simple blog application. Nothing special from the application, just a scaffold for Post.

Integration

Open app/views/posts/show.html.erb and add this code on the last line

<div id="comments"></div>

Then paste previously copied code snippet from Juvia on the bottom of the file

<script type="text/javascript" class="juvia">
(function() {
    var options = {
        container   : '(a CSS path)',
        site_key    : 'c8d0hus7yn2ruppaswbupkgq2culhro',
        topic_key   : location.path,
        topic_url   : location.href,
        topic_title : document.title || location.href,
        include_base: !window.Juvia,
        include_css : !window.Juvia
    };

    function makeQueryString(options) {
        var key, params = [];
        for (key in options) {
            params.push(
                encodeURIComponent(key) +
                '=' +
                encodeURIComponent(options[key]));
        }
        return params.join('&');
    }

    function makeApiUrl(options) {
        // Makes sure that each call generates a unique URL, otherwise
        // the browser may not actually perform the request.
        if (!('_juviaRequestCounter' in window)) {
            window._juviaRequestCounter = 0;
        }

        var result =
            'http://192.168.56.10:3000/api/show_topic.js' +
            '?_c=' + window._juviaRequestCounter +
            '&' + makeQueryString(options);
        window._juviaRequestCounter++;
        return result;
    }

    var s       = document.createElement('script');
    s.async     = true;
    s.type      = 'text/javascript';
    s.className = 'juvia';
    s.src       = makeApiUrl(options);
    (document.getElementsByTagName('head')[0] ||
     document.getElementsByTagName('body')[0]).appendChild(s);
})();
</script>

Change container : '(a CSS path)', to container : '#comments',

The container is a CSS path referring to the DOM element in which you want to embed the Juvia comments page, in our case #comments.

Other Juvia options you need to change are

  • topic_key - Unique identifier withing the context of a site. If you are running a blog site, you may want to set this to current category of your post.
  • topic_title - Specifies a title for topic_url. It is a good practice to set this with document_title value, that is why you need to make sure your document <title> is informative. My suggestion is using blog name and post title together, i.e "My Blog - Rails 3.2.1 up and running in 1 minute".

Conclusion

Juvia already provide you options on how to moderate comments, Akismet integration and email notification of new comments features that will save you hours of your development time.

While Juvia is a good (if not the best) alternative when you need to build a simple and complete commenting system for multiple web apps. Nevertheless, I don't think Juvia will handy when you need a commenting system for a single web app.