Ruby on rails 使用backbone.js从postgreSQL数据库中获取数据
因此,无法使用保留到我的postgres db中的主干来呈现这些相册(以rails索引列表方式):Ruby on rails 使用backbone.js从postgreSQL数据库中获取数据,ruby-on-rails,postgresql,backbone.js,Ruby On Rails,Postgresql,Backbone.js,因此,无法使用保留到我的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: "Da
[#<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">]
客户端渲染相册的我的主干部分如下所示
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;
},
});
但是当控制台记录相册时,它是一个长度为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查询,将检索正确的信息 服务器没有返回任何信息。将其配置为使用JSON响应,可以这样解决:
def index
@albums = Album.all
render :json => @albums
end
像这样的谢谢!一个信息链接,但我仍然从数据库中获取对象
app.collections.AlbumList = Backbone.Collection.extend({
model: app.models.Album,
url: '/albums'
});
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)
def index
@albums = Album.all
render :json => @albums
end