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 🙂
Share
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
Tripfor now:As you can see, the model has trip images attached to it via a
has_manyassociation. Lets have a quick look at theTripImagemodel, 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
TripControllerwhich has anewandcreateaction (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
paramshash. this makes sense when looking at the the file upload field within thenew.html.erbtemplate file (or in the partial you use for the fields on theTripmodel):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
Tripmodel: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.