Candy for MongoDB
I'm currently experimenting with Sinatra and MongoDB (through the Candy gem). I love the Candy coding style, but I'm having a few issues when I try to retrieve all of the Post objects. Here's my code:
require 'rubygems'
require 'sinatra'
require 'candy'
require 'haml'
Candy.db = "Miroir"
class Post
include Candy::Piece
end
class Posts
include Candy::Collection
collects :post
end
get '/' do
@posts = Posts.all
haml :index
end
When it renders index.haml, all I get is Post (4d0ac53d9b6d4202a3000001){}, and I can't retrieve any of the data. The haml is:
!!! 5
%html
%body
%strong Pos开发者_StackOverflow中文版ts
%ul
- @posts.each do |post|
%li= post
How can I iterate the titles of the posts in the ul? Appreciate the help.
There is a bug in Candy that requires you refresh the object before it'll populate the data when you do a find using a collection.
Do it like so:
- @posts.each do |post|
post.refresh
%li= post
精彩评论