2011-09-30 36 views
1

有人能給我一個使用CouchRest在CouchDB中存儲獨立附件的例子嗎?使用CouchRest的獨立附件

這是一個非Rails項目。所以不涉及CouchRest :: Model的東西會很好。

感謝, 馬諾

+0

我找到了答案!我正在用attachments.rb(在CouchRest源代碼)助手模塊中進行操作。實際使用的方法是put_attachment in database.rb ....行號313。 –

回答

0

它看起來像CouchRest GitHub的UsageOverview wiki page有一些基本的例子中,「附件」部分中:

contents = "<html><body>If you're happy and you know it, clap your hands.</body></html>" 
@db.put_attachment(doc, "happy.html", contents, :content_type => "text/html") 
# => {"ok"=>true, "id"=>"e0d70033da0fad3707fed320bd7e0770", "rev"=>"2-20635570c75e8b20f7a73fd1538f318d"} 

# NOTE: The document is not updated automatically with the attachment information 
pp doc.to_hash 
# {"_id"=>"e0d70033da0fad3707fed320bd7e0770", 
# "_rev"=>"1-cbb61d1f90f7c01b273737702265b6c8", 
# "key"=>"value", 
# "another key"=>"another value"} 

# If you try to fetch the attachment without getting the new state of the document, you will fail 

@db.fetch_attachment(doc, "happy.html") 
# => RestClient::ResourceNotFound: 404 Resource Not Found: {"error":"not_found","reason":"Document is missing attachment"} 

doc = @db.get(doc["_id"]) 

pp doc.to_hash 
# {"_id"=>"e0d70033da0fad3707fed320bd7e0770", 
# "_rev"=>"2-20635570c75e8b20f7a73fd1538f318d", 
# "key"=>"value", 
# "another key"=>"another value", 
# "_attachments"=> 
# {"happy.html"=> 
#  {"content_type"=>"text/html", 
#  "revpos"=>2, 
#  "digest"=>"md5-q3MreM1aJgfSLHGrJLdg4g==", 
#  "length"=>75, 
#  "stub"=>true}}} 

@db.fetch_attachment(doc, "happy.html") 
=> "<html><body>If you're happy and you know it, clap your hands.</body></html>" 

看到頁面的其餘部分更多的例子(包括刪除附件)。