2013-05-25 59 views
0

PostgreSQL數據庫的所以不能與骨幹被一直持續到我的Postgres數據庫獲取渲染這些相冊(在軌索引列表的方式):獲取數據了與Backbone.js的

[#<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">] 

服務器端我的索引方法是standard`

class AlbumsController < ApplicationController 
    def index 
    end 

和路由配置的相同

resources :albums 

客戶端我的骨幹部分來呈現專輯看起來像這樣

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; 
    }, 

}); 

繼承人的AlbumList收集

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

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

}); 

然而,當控制檯日誌相冊,它的0對象長度 我忘了在這裏將它鏈接到數據庫的一步嗎?謝謝!

而且這是GET請求的服務器發送

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) 

其中如果在控制檯上面的SQL查詢手動,將檢索正確的信息。

+0

像這樣的事情http://stackoverflow.com/questions/10619836/why-fetch-does-not-work/10620694#10620694 – nikoshr

+0

謝謝!信息鏈接,但我仍然從我的數據庫中獲取對象 –

回答

1

服務器沒有發回任何信息。配置它與JSON響應解決它像這樣:

def index 
    @albums = Album.all 

    render :json => @albums 
    end