i'm trying to get carrierwave + mongoid + gridfs to work for uploading images via padrino admin and then showing on front-end. at a loss as to glue everything. can someone help?
following https://blog.engineyard.com/2011/a-gentle-introduction-to-carrierwave/ and https://github.com/carrierwaveuploader/carrierwave-mongoid.
trying to persist an "Artwork" with an image upload returns:
NoMethodError at /admin/artworks/update/53a0eedcf2c7961066000002 undefined method `bson_dump' for # file: hash.rb location: block in bson_dump line: 15
mongofiles -d database list
returning empty.
question is: what's wrong with the code at the moment?
uploader: https://github.com/bcsantos/debug/blob/master/lib/uploader.rb
class Uploader < CarrierWave::Uploader::Base
##
# Image manipulator library:
#
include CarrierWave::RMagick
# include CarrierWave::ImageScience
# include CarrierWave::MiniMagick
##
# Storage type
#
storage :grid_fs
# configure do |config|
# config.fog_credentials = {
# :provider => 'XXX',
# :aws_access_key_id => 'YOUR_ACCESS_KEY',
# :aws_secret_access_key => 'YOUR_SECRET_KEY'
# }
# config.fog_directory = 'YOUR_BUCKET'
# end
# storage :fog
resize_to_limit(1024, 768)
## Manually set root
def root; File.join(Padrino.root,"public/"); end
##
# Directory where uploaded files will be stored (default is /public/uploads)
#
def store_dir
'content'
end
##
# Directory where uploaded temp files will be stored (default is [root]/tmp)
#
def cache_dir
Padrino.root("tmp")
end
##
# Default URL as a default if there hasn't been a file uploaded
#
# def default_url
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
##
# Process files as they are uploaded.
#
# process :resize_to_fit => [740, 580]
##
# Create different versions of your uploaded files
#
# version :header do
# process :resize_to_fill => [940, 250]
# version :thumb do
# process :resize_to_fill => [230, 85]
# end
# end
##
# White list of extensions which are allowed to be uploaded:
#
def extension_white_list
%w(jpg jpeg gif png)
end
##
# Override the filename of the uploaded files
#
# def filename
# "something.jpg" if original_filename
# end
end
-----------
upload https://github.com/bcsantos/debug/blob/master/app/models/upload.rb
class Upload
include Mongoid::Document
include Mongoid::Timestamps # adds created_at and updated_at fields
# field <name>, :type => <type>, :default => <value>
field :file, :type => String
field :created_at, :type => Time
attr_accessible :upload
mount_uploader :upload, Uploader
# You can define indexes on documents using the index macro:
# index :field <, :unique => true>
# You can create a composite key in mongoid to replace the default id using the key macro:
# key :field <, :another_field, :one_more ....>
end
-----------
model i want to associate the upload/picture with https://github.com/bcsantos/debug/blob/master/app/models/artwork.rb
class Artwork
include Mongoid::Document
include Mongoid::Timestamps # adds created_at and updated_at fields
# field <name>, :type => <type>, :default => <value>
field :name, :type => String
field :year, :type => Integer
field :author, :type => String
field :rent_price, :type => String
field :sale_price, :type => String
field :medium, :type => String
field :size, :type => String
field :colour, :type => String
field :picture, :type => Upload
field :thumbnail, :type => Upload
# You can define indexes on documents using the index macro:
# index :field <, :unique => true>
# You can create a composite key in mongoid to replace the default id using the key macro:
# key :field <, :another_field, :one_more ....>
end
-----------
controller to retrieve files from database (thanks @Darío)
TourApart::App.controllers do
get :gridfs, map: '/content/*' do
gridfs_file = Mongoid::GridFS[params[:splat]]
content_type gridfs_file.content_type
gridfs_file.data
end
error Mongoid::Errors::MongoidError do
halt 404, 'File not found'
end
end