Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So can't get these albums rendered(in a rails index list fashion) with backbone that are persisted into my postgres db:

 [#<Album id: 1, title: "abbey road", artist: "the beatles", created_at: "2013-05-24 20:35:44", updated_at: "2013-05-24 20:35:44">, #<Album id: 2, title: "Random Access Memories", artist: "Daft Punk", created_at: "2013-05-24 20:36:14", updated_at: "2013-05-24 20:36:14">, #<Album id: 3, title: "Illmatic", artist: "Nas", created_at: "2013-05-24 20:36:51", updated_at: "2013-05-24 20:36:51">] 

Server side my index method is standard`

  class AlbumsController < ApplicationController
    def index
    end

and routes are configured the same

resources :albums

Client side my backbone part to render the albums looks like this

app.views.Home = Backbone.View.extend({

  template: JST['templates/home'],

    render: function() {
    this.$el.html(this.template());

    // Find all the albums in the system
    var albums = new app.collections.AlbumList();
    albums.fetch();
    console.log(albums);
    var _this = this;

    albums.fetch({
      success: function(albums, response, options) {
        albums.forEach(function(album) {
        _this.$el.find("#albums").append("<li><a href='#' class='album-link' data-id='" + album.id + "'>" + album.title() + "</a></li>");
        });
      }
    });

    // Add a <li> element containing a link to each profile page
    return this;
  },

});

Heres the AlbumList collection

app.collections.AlbumList = Backbone.Collection.extend({

  model: app.models.Album,
  url: '/albums'

});

Yet when console log albums, its an object with 0 length Am I forgetting a step here to link it to the database? Thanks!

Further this is the GET request the server is sending

Started GET "/albums" for 127.0.0.1 at 2013-05-25 09:13:00 -0400
Processing by AlbumsController#index as JSON
  Album Load (0.2ms)  SELECT "albums".* FROM "albums" 
Completed 200 OK in 1ms (Views: 0.1ms | ActiveRecord: 0.2ms)

Which if the above SQL query in the console manually, will retrieve the correct info.

share|improve this question
Thanks! An informative link but I still get the objects out of my db – Keith Johnson yesterday

1 Answer

The server wasn't sending any information back. Configuring it to respond with JSON solved it like so:

  def index
    @albums = Album.all

    render :json => @albums
  end
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.