When I initialize a Backbone or a Javascript app, in a Rails view, I like to populate the template with the records that I am going to need. This is because if I dont populate this records in the view, then I have to fetch the record in the javascript app, so I need another call to the server :(.

I think a good way to produce, your JSON objects is with Jbuilder, because in this way you don’t have to override the “as_json” method. Then if you have a template that returns JSON, you can render it in your view.

The example

json.array!(@milestones) do |json, milestone|
  json.(milestone, :id, :name, :due_date)
  json.tasks milestone.tasks
<%= content_tag "div", class: "milestones",
  data: {
    milestones: render(template: "api/v1/milestones/index.json")
  } do %>

  <%= javascript_tag do %>
  <% end %>
<% end %>