Introduction
Dragonfly itself can be used to manage the media for your project as a whole, like paperclip . The question itself comes down to loading the file multiple times in the rails application. Some tutorials on this topic are available that can be easily adapted to models using Dragonfly to store specific files on them. I would suggest you study them and try to adapt them for your project.
However, I can provide a minimal example that I built for the rails 3.2 application, which is currently under development, which is not perfect (e.g. validation processing), but can give you some starting points.
Example
For reference, the basic idea is taken from here . This example runs with Rails 3.2.x.
Let's say you have a vacation database where users can create reports on vacation trips. They may leave a small description, as well as some photos.
Start by creating a simple ActiveRecord model for travel, just call her Trip :
class Trip < ActiveRecord::Base has_many :trip_images attr_accessible :description, :trip_images end
As you can see, the model has shutdown images attached to it using the has_many association. Let's take a quick look at the TripImage model, which uses a dragonfly to store a file in the content field:
class TripImage < ActiveRecord::Base attr_accessible :content, :trip_id belongs_to :trip_id image_accessor :content end
In the trip pattern, it saves the attachment file. You can place any restraining devices in this model, for example. file size or mime type.
Create a TripController that has a new and create action (you can generate this using scaffolding, if you want, it's not at all strange):
class TripController < ApplicationController def new @trip = Trip.new end def create @trip = Trip.new(params[:template])
There is nothing special here except for creating images from another record, except for the params hash. this makes sense when viewing the file upload field in the new.html.erb template new.html.erb (or in partial use for fields in the Trip model):
[...] <%= f.file_field :trip_images, :name => 'images[]', :multiple => true %> [...]
This should work for now, but there are no restrictions for the images right now. You can limit the number of images on the server side using a custom validator in the Trip model:
class Trip < ActiveRecord::Base has_many :trip_images attr_accessible :description, :trip_images validate :image_count_in_bounds, :on => :create protected def image_count_in_bounds return if trip_images.blank? errors.add("Only 10 images are allowed!") if trip_images.length > 10 end end
I leave it for you, but you can also use client-side checks in the file field, the general idea is to check the files when changing the file field (in CoffeeScript):
jQuery -> $('#file_field_id').change () ->
Summary
You can do a lot from existing tutorials, because dragonfly doesn't behave differently with other solutions when it comes to file downloads. However, if you like something more interesting, I would suggest jQuery Fileupload , like so many others, in front of me.
In any case, I hope I can give some insight.