# Cachai A clean way to add support for comments and pingbacks in your Rack app. Si pos weon. ## Setting up First you need to create a config/database.yml file, as you'd have on a regular Rails app, or if using Sinatra::ActiveRecord. # database.yml development: adapter: sqlite3 database: db/development.sqlite3 pool: 16 Now insert the middleware before the app, either in your rackup file or in the app itself. We'll go with method one. # rackup.ru require 'sinatra' require 'cachai' map '/' do use Cachai::Middleware, { domain: 'yourdomain.com' } run Sinatra::Application end Now, the next time the app is loaded, Cachai will set up the necessary tables in the database provided. Basically a `posts` table and a `responses` one. ## Posting new comments Now, to send our first comment to Cachai, let's insert a form within your blog post view. # views/blog/post.erb (or something like it) ...
Now load your app and try submitting a comment. It should work. ## Posting comment using AJAX Using Javascript, you'd do something like this: // app.js $(function() { $('#comment-form').on('submit', function(e) { e.preventDefault(); var data = { author_name : this.author_name.value, author_email : this.author_email.value, author_url : this.author_url.value, content : this.content.value, protocol : window.location.protocol, domain : 'yourdomain.com', path : encodeURI(window.location.pathname) } $.ajax({ type : 'post', data : JSON.stringify(data), url : '/comments.json', success : function() { alert('Thanks!') }, error : function() { alert('Oh rats. Try again please.') }, }) }) }) ## Reading comments via AJAX You get them using GET /comments.json. // app.js var domain = 'yourdomain.com', path = encodeURI(window.location.pathname), url = '/comments.json?callback=?&domain=' + domain + '&path=' + path; $.ajax({ dataType : 'json', url : url, success : function(list) { /* render comments */ }), error : function(err) { alert('Damn!') } }); } Or if you wish to insert them in the view itself. # views/blog/post.erb