This is a quick screencast on how to use Backbone.js with a Ruby on Rails application. The screencast covers the simple example of submitting a form, saving the data to the database, and displaying the records in a table. The repo for the backbone-demo app I created for the screencast is on github. [UPDATE: if you are using Backbone 0.5.x make sure you change the word refresh to reset.]

I recently started using Backbone.js and have found it to be extremely useful for keeping my Javascript organized. One of the benefits of using Backbone that I didn't realize until later is that it makes you start building a RESTful API without you even focusing on that as an outcome. Awesome.

One of the gotchas (which is covered n the Backbone.js site) is that Rails returns a JSON object that looks like this:

{ dog: { name: "Fido", age: "2" }}

While Backbone just wants to see:

{ name: "Fido", age: "2" }

In order to get Rails to play well with Backbone you need to tell Rails not to return the root in the JSON return. This is accomplished by adding the following line to an initializer.

ActiveRecord::Base.include_root_in_json = false

Backbone.js is really great stuff and I look forward to using it more in my applications. You should check it out if you haven't already.