2011-09-30 4 views
1

CouchDB에서 독립 실행 형 첨부 파일을 저장하는 데 CouchRest를 사용하는 예가 있습니까?CouchRest를 사용하는 독립 실행 형 첨부 파일

비 Rails 프로젝트 용입니다. 그래서 CouchRest :: Model을 포함하지 않는 무언가가 좋을 것입니다.

덕분에, 마노

+0

답변을 찾았습니다! 나는 attachments.rb (CouchRest 소스에서) 도우미 모듈로 주위를 망쳤다. 실제 사용하는 방법은 database.rb .... line number 313에 put_attachment입니다. –

답변

0

그것은 "첨부 파일"섹션에서 UsageOverview wiki page 몇 가지 기본적인 예제를 가지고 CouchRest의 github에 다음과 같습니다 (

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>" 

더 많은 예제 페이지의 나머지 부분을 참조하십시오 포함하여 삭제를 첨부 파일).

관련 문제