RESTful JSON APIs with Rails
In this final chapter, we will take a break from this book's appropriately Backbone-centric point of view, and shift our perspective to the server and its API. There are so many popular server platforms used with Backbone that it would be impractical to attempt to cover all of them. The server-side code in this chapter will feature examples from Ruby on Rails, which is the framework used in the DocumentCloud project that is the birthplace of Backbone.
If you do not use Rails for your server platform, however, please do not skip the chapter. It is likely to hold more value for non-Rails developers than for those using Rails, since for Rails projects the Backbone defaults should just work (at least to a certain extent.) If you have an allergic reaction to Rails or Ruby, try to focus on the intent of the server-side examples, rather than the details. There is not all that much Rails code in any case: The star of our show is JSON.
The intricacies of JSON API design are not likely to be the most exciting part of building a full-stack Backbone application, although I hope our time here will be well spent. Like a relational database schema, your API is often costly to change once in use, and getting it right the first time can pay steady dividends toward your project's productivity level and progress.
Backbone's built-in support for server integration is oriented toward the RESTful style of JSON API that is generated by Rails when you scaffold a resource. In the first part of this chapter, we'll discuss this style in detail, so that you know what you need to build if you want to leverage the built-in support with a platform other than Rails. Also, we'll discuss seeding JSON data for your Backbone application as part of the initial page load. Finally, we'll take a brief look at some topics that fall outside of Backbone's RESTful support, such as error messaging and pagination.