Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 轨道&x2B;CouchDb二进制文件上载到数据库_Ruby On Rails_Couchdb_Couchpotato - Fatal编程技术网

Ruby on rails 轨道&x2B;CouchDb二进制文件上载到数据库

Ruby on rails 轨道&x2B;CouchDb二进制文件上载到数据库,ruby-on-rails,couchdb,couchpotato,Ruby On Rails,Couchdb,Couchpotato,Simple Stored基于CouchPotato顶部,用于在rails中处理CouchDb。在尝试将文件上传到couchdb时,我们尝试使用base64、json post,但似乎没有任何东西可以正常工作;我们正在尝试上载到已存储文档的_attachments属性 有这样的模型: class Patient include SimplyStored::Couch end 在控制器中,通过更新操作接收文件 def update @patient = Patient.find(pa

Simple Stored基于CouchPotato顶部,用于在rails中处理CouchDb。在尝试将文件上传到couchdb时,我们尝试使用base64、json post,但似乎没有任何东西可以正常工作;我们正在尝试上载到已存储文档的_attachments属性

有这样的模型:

class Patient
  include SimplyStored::Couch
end
在控制器中,通过更新操作接收文件

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
现在有趣的是,我可以看到@patient.\u Actachments拥有文件本身,这就是在.save之后在渲染中返回的内容;但它实际上并没有将其保存在couchdb数据库中


你知道为什么我不保存,或者我应该试着将_附件推送到couchdb数据库吗?(顺便说一句,它总是返回一个500错误:()

我不知道Ruby和couchpotato,但我认为你不需要以附件为基础。只要读取二进制信息并将其写入请求即可。
my 2cents.:

解决方案非常简单,基于Couch马铃薯网站,您实际上不需要将其转换为base64这里是代码工作的示例

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
由于这些值位于[:patient][:_attachments]参数中,因此您只需将其作为另一个已测试并正在工作的参数传递即可

您还需要将患者模型定义为

    property :_attachments
不知道是否需要,但我做到了

我知道我不该要钱,但因为我为你工作了四天,所以每小时只有100比索。。办公室见

干杯


lols

生成了一条无效的json指令,但它无法解析。我希望他至少在这上面请了几杯啤酒:)