public
Created

  • Download Gist
carrierwave.rb
Ruby
1 2 3 4 5 6 7
# config/initializers/carrierwave.rb
CarrierWave.configure do |config|
config.grid_fs_database = "database_name"
config.grid_fs_host = 'localhost'
config.grid_fs_access_url = "/uploads"
config.storage = :grid_fs
end
gridfs_serve_image.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# app/metal/gridfs_serve_image.rb
require 'mongo'
 
require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)
 
class GridfsServeImage
def self.call(env)
if env["PATH_INFO"] =~ /^\/uploads\/(.+)$/
begin
Mongo::GridFileSystem.new(Mongoid.database).open($1, 'r') do |file|
[200, { 'Content-Type' => file.content_type }, [file.read]]
end
rescue
[404, { 'Content-Type' => 'text/plain' }, ['File not found.']]
end
else
[404, { "Content-Type" => "text/html", "X-Cascade" => "pass" }, ["Not Found"]]
end
end
end
show.html.haml
Haml
1 2
-# app/views/something/show.html.haml
= image_tag @something.photo.url

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.