2014-01-06 52 views
0

我正在使用Rails4,Mongoid4和Gridfs。我; M無法連接GridFS的文件系統Monogid4 Gridfs連接失敗

class GridfsController < ApplicationController 
    def serve 
    gridfs_path = env["PATH_INFO"].gsub("/uploads/", "") 
    begin 
    gridfs_file = Mongo::GridFileSystem.new(Mongo::DB.new('database_name', Mongo::Connection.new('localhost'))).open(gridfs_path, 'r') 
    self.response_body = gridfs_file.read 
    self.content_type = gridfs_file.content_type 
    rescue Exception => e 
    self.status = :file_not_found 
    self.content_type = 'text/plain' 
    self.response_body = '' 
    raise e 
    end 
end 
end 

得到這個錯誤

NameError(未初始化常數GridfsController ::蒙戈):
應用程序/控制器/ gridfs_controller.rb:7:在`服務」

回答

0

Mongoid不使用「官方」Ruby驅動程序與MongoDB對話,這就是Mongo::GridFileSystem的來源。 Mongoid使用Moped與MongoDB交談,而Moped對GridFS一無所知。

AFAIK通常GridFS的解決方案是使用mongoid-grid_fs交談GridFS的:

self.response_body = Mongoid::GridFs[gridfs_path].data 

,或者如果你有id替代路徑:

self.response_body = Mongoid::GridFs.get(gridfs_id).data 
+0

它工作正常,非常感謝你 – sukendhar

0

還有就是GridFS的的實現這裏的摩托車驅動程序的規格:moped-gridfs

這比加載兩個驅動程序(腳踏車和mongo-ruby-driv呃)