Integrating Backbone JS with Rails Application
In this section we will talk about easily integrating backbone js with a rails application. If you want to integrate backone js with your rails application in a rails familiar way, rails-backbone gem is best to use. Lets simply see how easy it is.
1) Add rails-backbone gem to your Gemfile.
2) Then run bundle.
3) After this to create the backbone directory structure you need to run this command:
rails g backbone:install
Models: Suppose you have a product model with price and name, then you can create a corresponding model in backbone like this:
rails g backbone:model Product name:string price:integer
# This will create a model file like this
class BackboneApp.Models.Product extends Backbone.Model
class BackboneApp.Collections.ProductsCollection extends Backbone.Collection
Routers: There is generator available also for routers, suppose you want to create the routes for the product model you can do that like this:
rails g backbone:router products index
# It will create these files:
Scaffold: Just like rails scaffold if you want to generate models, views and routes in one hit, this gem also supports scaffold generators. You can create everything at one hit like this:
rails g backbone:scaffold item quantity:integer price:integer
# It will generate these files:
Thus you can see that its very easy here to create the setup for backbone in your rails app. For learning more about backbone you can go through the link: http://backbonejs.org/