载波+ Mongoid + gridfs + Padrino管理映像上传 [英] Carrierwave + Mongoid + gridfs + Padrino admin image upload

查看:101
本文介绍了载波+ Mongoid + gridfs + Padrino管理映像上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使载波+ mongoid + gridfs能够通过padrino管理员上传图片,然后在前端显示.粘糊糊的一切.有人可以帮忙吗?

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?

跟随 https://blog.engineyard.com/2011/a-gentle-introduction-to-carrierwave/ https://github.com/carrierwaveuploader/载波-杂种动物.

尝试通过上传的图片保留艺术品"会返回:

trying to persist an "Artwork" with an image upload returns:

在/admin/artworks/update/53a0eedcf2c7961066000002的NoMethodError #文件的未定义方法" bson_dump ": hash.rb位置: bson_dump 行中的块:15

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

返回空.

问题是:目前代码有什么问题?

question is: what's wrong with the code at the moment?

上传器: 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)

-----------

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

推荐答案

首先,鉴于此模型设计,您的Artwork模型没有正确嵌入Upload文档.相反,应像这样嵌入picturethumbnail字段:

First, given this model design, your Artwork model isn't properly embedding the Upload document. It should instead embed the picture and thumbnail field like so:

class Artwork
  # ...
  embeds_one :picture, class_name: "Upload"
  embeds_one :thumbnail, class_name: "Upload"
  # ...
end

除了,我不明白为什么首先要有一个Upload文档.鉴于您的设计,这似乎是不必要的. Uploader包含您要存储在Upload模型中的所有信息.例如,您的Artwork模型可能看起来像这样(并且您可以将Upload模型全部转储在一起):

Aside, I don't understand why you have an Upload document in the first place. It seems unnecessary given your design. The Uploader holds all the information that you're trying to store in your Upload model. For example, your Artwork model could instead just look like this (and you can dump the Upload model all together):

class Artwork
  # ...
  mount_uploader :picture, Uploader
  mount_uploader :thumbnail, Uploader
  # ...
end

您的picturethumbnail字段将保存诸如更新日期,文件名,文件日期等内容.

Your picture and thumbnail field will hold things like the date it was updated, the filename, the file date, etc.

此外,您似乎正在尝试手动管理thumbnail.我认为缩略图是picture的较小版本. Carrierwave也可以为您处理此问题:

Also, it looks like you're trying to manually manage the thumbnail. I presume the thumbnail is a smaller version of the picture. Carrierwave can handle this for you too:

class Artwork
  # ...
  mount_uploader :picture, Uploader
  # ...
end

然后将类似的内容添加到您的Uploader:

Then add something like this to your Uploader:

class Uploader < CarrierWave::Uploader::Base
  # ...
  version :thumbnail do
    process resize_to_fill: [160, 120]
  end
  # ...
end

这样,您将拥有两个不同版本的Artwork#pictureArtwork#picture_thumbnail访问器.

That way, you'll have a Artwork#picture and a Artwork#picture_thumbnail accessor for the two different versions.

更多示例:

  • Artwork#picture.read-获取图像数据
  • Artwork#picture.file-获取图像文件
  • Artwork#picture_thumbnail.file-获取缩略图版本的文件
  • Artwork#picture.file.content_type-是的,content_type
  • Artwork#picture.read - get the image data
  • Artwork#picture.file - get the image file
  • Artwork#picture_thumbnail.file - get the thumbnail version's file
  • Artwork#picture.file.content_type - yup, the content_type
  • etc

最后,如果您使用的是载波-mongoid,则无需直接在控制器中访问Mongoid::GridFS.查找图稿,然后访问picture字段.让GridFS留在幕后.

Lastly, if you're using carrierwave-mongoid, there's no need to access Mongoid::GridFS directly in your controller. Look-up the artwork and just access the picture field. Let GridFS stay behind the scenes.

此外,我建议在CarrierWave::RMagick上使用CarrierWave::MiniMagick.从长远来看,这只会使事情变得容易,IMO,但它要求您在计算机上安装ImageMagick.

Aside, I would recommend using CarrierWave::MiniMagick over CarrierWave::RMagick. It just makes things easier in the long run, IMO, but it requires that you have ImageMagick installed on your machine.

这篇关于载波+ Mongoid + gridfs + Padrino管理映像上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆