我试图让carrierwave + mongoid + gridfs通过padrino管理员上传图片,然后在前端显示。无所适从,无所适从。有人可以帮忙吗?Carrierwave + Mongoid + gridfs + Padrino管理图像上传
以下https://blog.engineyard.com/2011/a-gentle-introduction-to-carrierwave/和https://github.com/carrierwaveuploader/carrierwave-mongoid。
努力坚持的 “艺术品” 与图像上传回报:
NoMethodError在/管理/艺术品/更新/ 53a0eedcf2c7961066000002 未定义的方法`bson_dump'为#文件: hash.rb位置:块bson_dump线:15
mongofiles -d database list
返回空。
问题是:此刻的代码有什么问题?
上传: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
-----------
上传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
-----------
模型我想联想上传/图片与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
-----------
控制器从数据库中检索文件(感谢@达里奥)
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