Two tricks for getting Backbone.js to play well with Ruby on Rails

Backbone.js is a great library for creating single page applications that consume RESTful JSON services. Ruby on Rails does well at generating RESTful APIs and speaking JSON. In order to get the Rails working really well with Backbone, there are two tricks to do.

First, add the following line to the application controller to make the output look like what backbone expects.

Second take advantage of the : only option in to_json to limit the attributes included in the serialised json object. The documentation for this isn’t quite there in the latest rails, but the 2.3 to_json method describes it. http://apidock.com/rails/ActiveRecord/Serialization/to_json.

With these two simple tricks rails will generate the json that backbone wants, and you’ll have a happy front-end developer.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>