因此,无法使用持久存储在我的postgres db中的主干(以rails索引列表方式)呈现这些专辑:
[#<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">]
服务器端我的索引方法是标准的
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 :(得分:1)
服务器没有发回任何信息。配置它以JSON响应解决了它:
def index
@albums = Album.all
render :json => @albums
end