How to Add Comments to Rails App with Commontator

08 Aug 2014

Posted by Milos Dolobac

Last time we’ve added voting. This time I will show you how easy can be adding comments with Commontator.

Why to Use Commontator

Commontator has following advantages:

  • It includes comments count
  • Is easy to configure
  • It includes creating, editing and deleting comments
  • You can add voting with acts_as_votable
  • Is customizable

How to Install Commontator

First thing we need to do is to add commontator gem to Gemfile

Gemfile

gem 'commontator'

Then install it with Bundler

bundle install

Now you can create Commontator configuration and migrations

rake commontator:install

And move migrations to database

rake db:migrate

How to Use Commontator in Our Models

Let’s assume you’ve got two models, first is User and second is a Link

User should be able to post comments, so add this line to your User model.

app/models/user.rb

class User < ActiveRecord::Base
  
  acts_as_commontator

We want to show comments under each link, so add acts_as_commontable helper to Link model.

app/models/link.rb

class Link < ActiveRecord::Base

acts_as_commontable

Adding Comment’s route

Next thing we need to is to add commontator’s route.

config/routes.rb

mount Commontator::Engine => '/commontator'

Showing Comments

So we’ve added commontator’s helpers to our model, so we can add commontator’s thread to our views. Let’s assume we’ve got index action in links_controller.

app/controllers/links_controller.rb

class LinksController < ApplicationController

def index
  @links = Link.all
end

Let’s add commontator’s thread to our views.

app/views/links/index.html.slim

- @links.each do |link|
  = commontator_thread(link)

Now you should see comments count:

how to add comments count in Rails

Click on show comments, you should see new comment link.

how to add new comment in Rails

Fill in new comment form and look at the comment that we’ve created:

how to show comment in Rails

Looks nice isn’t it?

Replacing “Anonymous” with username

You certainly noticed that when you’ve created comment, it shows Anonymous instead of username.

Let’s fix that. Find this line in commontator initializer

config/initializers/commontator.rb

config.user_name_proc = lambda { |user| I18n.t('commontator.anonymous') }

Now if you have column username in users table replace previous line with following:

config.user_name_proc = lambda { |user| user.username }

Now if you look at your comments in a browser you should see user’s name instead of Anonymous.

Customization

Let’s assume you want to customize views. Run this command:

rake commontator:copy:views

You should see that commontator folder was created in our views folder.

That’s all for now. I hope that you liked my tutorial. If you don’t understand anything about post, feel free to ask in discussion. And if you like it, share it with your favorite social network.

comments powered by Disqus