所以不能用持久化到我的 postgres 数据库中的主干来渲染这些专辑(以 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 查询,将检索到正确的信息。