Skip to content
langalex edited this page Sep 13, 2010 · 3 revisions

As described in the readme you can store attachments in a document like this:

user._attachments = {'photo' => {'data' => imagefile.read, 'content_type' => 'image/png'}}

If you want to fetch the attachment from within Rails, you can use couch_rest:

CouchPotato.couchrest_database.fetch_attachment(_id, "photo")

If you want to serve images (or any other data) in a rack based webapp (like Rails), a better approach is to use the rack-streaming-proxy gem. In a Rails 2.3.x app add the following code to your environment.rb

config.gem 'rack-streaming-proxy'
require 'rack/streaming_proxy'
config.middleware.use Rack::StreamingProxy do |request|
  if(path = request.path.scan(/couch_assets\/(.+)/).first)
    [CouchPotato.full_url_to_database, URI.encode(path.first)].join('/')
  end
end

and in your application_helper.rb:

def couch_image_url(object, image_id)
  attachment = (object._attachments || {})[image_id]
  "/couch_assets/#{object.id}/#{image_id}"
end

to embed images in in your views:

<%= image_tag couch_image_url(user, 'photo') %>
Clone this wiki locally