我试图让carrierwave+mongoid+gridfs通过padrino admin上传图像,然后在前端显示把所有东西都粘在一起有人能帮忙吗?
在https://blog.engineyard.com/2011/a-gentle-introduction-to-carrierwave/和https://github.com/carrierwaveuploader/carrierwave-mongoid之后。
尝试通过图像上载保存“Artwork”返回:
nomethoderror,地址为/admin/artworks/update/53a0eedcf2c796106600002
文件的未定义方法“bson_dump”:
hash.rb位置:bson_转储行中的块: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
—————---
从数据库中检索文件的控制器(感谢@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
最佳答案
首先,考虑到这个模型设计,您的艺术模型没有正确地嵌入Upload
文档。它应该像这样嵌入picture
和thumbnail
字段:
class Artwork
# ...
embeds_one :picture, class_name: "Upload"
embeds_one :thumbnail, class_name: "Upload"
# ...
end
另外,我不明白为什么你一开始就有一个
Upload
文档。考虑到你的设计,似乎没有必要。Uploader
包含您试图存储在Upload
模型中的所有信息。例如,您的Artwork
模型可以改为如下所示(您可以将Upload
模型全部转储在一起):class Artwork
# ...
mount_uploader :picture, Uploader
mount_uploader :thumbnail, Uploader
# ...
end
您的
picture
和thumbnail
字段将包含更新日期、文件名、文件日期等内容。而且,看起来您正在尝试手动管理
thumbnail
。我认为缩略图是picture
的较小版本。Carrierwave也能帮你处理:class Artwork
# ...
mount_uploader :picture, Uploader
# ...
end
然后在您的
Uploader
中添加类似的内容:class Uploader < CarrierWave::Uploader::Base
# ...
version :thumbnail do
process resize_to_fill: [160, 120]
end
# ...
end
这样,对于两个不同的版本,您将拥有一个
Artwork#picture
和一个Artwork#picture_thumbnail
访问器。更多示例:
Artwork#picture.read
-获取图像数据Artwork#picture.file
-获取图像文件Artwork#picture_thumbnail.file
-获取缩略图版本的文件Artwork#picture.file.content_type
-是的,内容类型等
最后,如果您使用的是carrierwave mongoid,则无需直接在控制器中访问
Mongoid::GridFS
查找图片并访问picture
字段。让gridf呆在幕后。另外,我建议使用
CarrierWave::MiniMagick
而不是CarrierWave::RMagick
。从长远来看,这只会让事情变得更容易,imo,但它要求您在机器上安装imagemagick。关于ruby - 载波+ Mongoid + gridfs + Padrino管理镜像上传,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24203977/