使用蜻蜓上传多张图片
我正在尝试使用rails3中的蜻蜓上传多个图像。我搜索了一些教程,但没有找到。我找到了一个使用 Carrierwave 上传多个图像的教程,但找不到 Dragonfly 的运气..请提供帮助:)
i was trying for multiple image upload with dragonfly in rails3. i searched for some tutorials, but couldn't find any. i found a tutorial for multiple image upload with Carrierwave, but couldnt find luck with dragonfly .. any help please :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
前言
Dragonfly 本身通常可以用来管理项目的媒体,类似于 回形针。问题本身可以归结为 Rails 应用程序中的多个文件上传。有关此主题的一些教程可用,可以轻松地适应使用 Dragonfly 存储特定文件的模型。我建议您研究一下这些内容并尝试使它们适合您的项目。
不过,我可以提供一个我为当前正在开发的 Rails 3.2 应用程序构建的最小示例,该示例并不完美(例如验证处理),但可以为您提供一些起点。
示例
仅供参考,基本思想取自
假设您有一个假期数据库,用户可以在其中创建有关他们度假的旅行报告。他们可能会留下一些简短的描述以及一些图片。
首先为行程构建一个简单的基于 ActiveRecord 的模型,暂时将其称为
Trip
:如您所见,该模型通过
has_many
附加了行程图像> 协会。让我们快速浏览一下TripImage
模型,它使用 Dragonfly 将文件存储在内容字段中:旅行图像本身存储文件附件。您可以在此模型中设置任何限制,例如文件大小或 MIME 类型。
让我们创建一个
TripController
,它有一个new
和create
操作(如果您愿意,您可以通过脚手架生成它,到目前为止,这没什么花哨的) :这里没有什么特别的,除了从
params
哈希之外的另一个条目创建图像之外。当查看new.html.erb
模板文件中的文件上传字段(或在Trip
模型上用于字段的部分)时,这是有意义的:这暂时应该有效,但是,目前对此图像没有限制。您可以通过自定义验证器<来限制服务器端的图像数量/a> 在
Trip
模型上:我将其留给您,但您也可以在文件字段上使用客户端验证,一般想法是在更改文件字段时检查文件(在 CoffeeScript 中):
总结
您可以根据现有教程构建很多内容,因为在上传文件时,蜻蜓的行为与其他解决方案没有什么不同。只是。但是,如果您想要更高级的东西,我建议 jQuery Fileupload,正如我之前的许多人所做的那样。
无论如何,我希望我能提供一些见解。
Preface
Dragonfly itself can be used to manage media for your project in general, similar to paperclip. The question itself boils down to the multiple file upload within a rails application. The some tutorials on this topic available, which can easily be adapted to models using Dragonfly for storing specific files on them. I would suggest you look into those and try to adapt them for your project.
However, I can present a minimum example which i built for a rails 3.2 app currently in development, which isn't perfect (validation handling for example), but can give you some starting points.
Example
Just for reference, the essential idea is taken from here. This example is done with Rails 3.2.x.
Let's say you have a vacation database, where users may create trip reports on vacations they took. They may leave a small description, as well as some pictures.
Start out by building a simple ActiveRecord based model for the trips, lets just call it
Trip
for now:As you can see, the model has trip images attached to it via a
has_many
association. Lets have a quick look at theTripImage
model, which uses dragonfly for having the file stored in the content field:The trip image it self stores the file attachment. You may place any restrains within this model, e.g. file size or mime type.
Let's create a
TripController
which has anew
andcreate
action (you can generate this via scaffolding if you like, it is by far nothing fancy):Nothing special here, with the exception of creating the images from another entry than the
params
hash. this makes sense when looking at the the file upload field within thenew.html.erb
template file (or in the partial you use for the fields on theTrip
model):This should work for the moment, however, there are no limitations for the images on this right now. You can restrict the number of images on the server side via a custom validator on the
Trip
model:I leave this up to you, but you could also use client side validations on the file field, the general idea would be to check the files upon changing the file field (in CoffeeScript):
Summary
You can build a lot out of existing tutorials, as dragonfly does not behave that differently to other solutions when it comes to just to uploading files. However, if you'd like something fancier, I'd suggest jQuery Fileupload, as many others have before me.
Anyways, I hope I could provide some insight.