Question

Simple-Stored is based on top of CouchPotato for handling CouchDb in rails. While trying to upload files to the couchdb we have tryied using base64, json post and nothing seems to work quite right; we are trying to upload to the _attachments propertie of a document already stored.

Having the model like this :

class Patient
  include SimplyStored::Couch
end

and in the controller while receiving the file trough the update action

def update
    @patient = Patient.find(params[:id])
    if params[:patient][:_attachments] 
        attachement = params[:patient][:_attachments]
        filedata = attachement.tempfile.read
        data = Base64.encode64(filedata).gsub(/\n/, '')
        type = attachement.content_type
        or_name = attachement.original_filename
        @patient._attachments = {or_name => {'data' => data, 'content_type' => type}}
        @patient.save
        return render :json => @patient._attachments
    end 
end

Now the fun part is that I can see that @patient._acttachments has the file itself and that is what is returning in the render after the .save; but it is not actually saving it on the couchdb database.

Any ideas why is not doing the save or should I try to just push the _attachment to the couchdb database. ? (which by the way always returns a 500 error :( )

Was it helpful?

Solution

the solution it's very simple, based on the couchpotato website, you actually don't need to convert it to base64 here is the example of code working

if params[:patient][:_attachments] 
            attachement = params[:patient][:_attachments]
            data = attachement.tempfile.read
            type = attachement.content_type
            or_name = attachement.original_filename
            params[:patient][:_attachments] = {or_name => {'data' => data, 'content_type' => type}}
end 
  if @patient.update_attributes(params[:patient]) #blah blah blah

since the values are in the [:patient][:_attachments] params, you just need to pass it as another param tested and working.

Also you need to define your patients model as

    property :_attachments

dunno if that is required but I did it.

I know I should not ask for money but since I WORK FOUR YOU its only 100 pesos/hour.. see you at the office

cheers

lols

OTHER TIPS

I donno about the Ruby and couchpotato, but I don't think you need to Base64 your attachment. Just read the binary info and write it to request. my 2cents. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top