Rails Meets Backbone
As client-side applications have become more and more popular, so have client-side frameworks such as Backbone, Ember, and Angular. While Rails is great for traditional applications, where the majority of the work is done on the server, it does not provide many tools for developing dynamic single-page applications.
At first glance, it may seem a little redundant to use a back-end framework like Rails concurrently with a front-end framework like Backbone, since both follow the MV* pattern. In reality, the two actually have completely different responsibilities that do not conflict with each other.
In a nutshell, when used together, the back-end framework helps manage interactions with the database while the front-end framework helps manage interactions with the user.
I created the diagram above to give a high-level overview of how Rails and Backbone are able to work well together. The left-hand side is the server-side and the right-hand side is the client-side.
Going from right to left:
When a user interacts with the webpage, the client-side router calls the model or collection to fetch data from the server. On the server, the router determines which controller method should handle the request, and the controller method then creates a model, which is responsible for querying the database. The model then passes the data back to the controller, which renders it in an easily-parse-able format. The server-side router passes the response back to the client-side model, which is then fed into a view. The view uses this data to fill in the template, and renders the resulting html back to the client-side router. The html is then inserted into the correct section of the webpage without the page ever needing to reload completely.