首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用CouchRest的独立附件

使用CouchRest的独立附件
EN

Stack Overflow用户
提问于 2011-09-30 14:47:28
回答 1查看 635关注 0票数 1

有人能给我一个在CouchDB中使用CouchRest存储独立附件的例子吗?

这是针对非Rails项目的。所以不涉及CouchRest::Model的东西会更好。

谢谢你,马诺

EN

回答 1

Stack Overflow用户

发布于 2012-07-30 05:39:44

在“附件”部分,看起来CouchRest github UsageOverview wiki page有一些基本的例子:

代码语言:javascript
复制
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>"

有关更多示例(包括删除附件),请参阅页面的其余部分。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7606838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档