rails3, gridfs and mongomapper: how to serve files? send_data?

1.4k views Asked by At

i am currently developing a rails3 app with mongomapper and file storage in gridfs. after some trying around, i found grip and currently also use it in the app for storing the data. so far, so good - now i am trying to get my head around serving the files to the user -- what would be the best/fastest way to achieve that?

from: http://railstips.org/blog/archives/2009/12/23/getting-a-grip-on-gridfs/

there seem to be 2 ways:

  • send_data from ruby/rails - is this a recommended way? fast enough? (i want to use passenger in the deploy setup)
  • writing a rails metal (see http://gist.github.com/264077) - any comments or hints on how to use that with rails3?

any other ideas or even examples? thanks a lot!

1

There are 1 answers

0
parasew On

I am using rack-gridfs for exactly that purpose to serve files to the browser. The plugin is from jnunemaker, who also wrote mongomapper. you might also want to take a look at my fork of grip, where i added some more documentation and methods to see if an attachment exists:

github.com/parasew/grip

for rails3, you need the following steps to get rack-gridfs working:

add the following to your Gemfile

gem "jnunemaker-rack-gridfs", :git =>"git://github.com/jnunemaker/rack-gridfs.git"

then add these lines to your application.rb (in the class Application < Rails::Application block) - replace the values with your actual mongodb setup.

require 'rack/gridfs'
config.middleware.use Rack::GridFS, :hostname => 'localhost', :port => 27017, :database => "yourdatabase", :prefix => 'gridfs'