Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h1>Preface</h1> <p><a href="https://github.com/markevans/dragonfly/" rel="nofollow noreferrer">Dragonfly</a> itself can be used to manage media for your project in general, similar to <a href="https://github.com/thoughtbot/paperclip/" rel="nofollow noreferrer">paperclip</a>. 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.</p> <p>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.</p> <h1>Example</h1> <p>Just for reference, the essential idea is taken from <a href="http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/" rel="nofollow noreferrer">here</a>. This example is done with Rails 3.2.x.</p> <p>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.</p> <p>Start out by building a simple ActiveRecord based model for the trips, lets just call it <code>Trip</code> for now:</p> <pre><code>class Trip &lt; ActiveRecord::Base has_many :trip_images attr_accessible :description, :trip_images end </code></pre> <p>As you can see, the model has trip images attached to it via a <code>has_many</code> association. Lets have a quick look at the <code>TripImage</code> model, which uses dragonfly for having the file stored in the content field:</p> <pre><code>class TripImage &lt; ActiveRecord::Base attr_accessible :content, :trip_id belongs_to :trip_id image_accessor :content end </code></pre> <p>The trip image it self stores the file attachment. You may place any restrains within this model, e.g. file size or mime type.</p> <p>Let's create a <code>TripController</code> which has a <code>new</code> and <code>create</code> action (you can generate this via scaffolding if you like, it is by far nothing fancy):</p> <pre><code>class TripController &lt; ApplicationController def new @trip = Trip.new end def create @trip = Trip.new(params[:template]) #create the images from the params unless params[:images].nil? params[:images].each do |image| @trip.trip_images &lt;&lt; TripImages.create(:content =&gt; image) end if @trip.save [...] end end </code></pre> <p>Nothing special here, with the exception of creating the images from another entry than the <code>params</code> hash. this makes sense when looking at the the file upload field within the <code>new.html.erb</code> template file (or in the partial you use for the fields on the <code>Trip</code> model):</p> <pre><code>[...] &lt;%= f.file_field :trip_images, :name =&gt; 'images[]', :multiple =&gt; true %&gt; [...] </code></pre> <p>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 <a href="https://stackoverflow.com/questions/2263267/limit-number-of-objects-in-has-many-association">custom validator</a> on the <code>Trip</code> model:</p> <pre><code>class Trip &lt; ActiveRecord::Base has_many :trip_images attr_accessible :description, :trip_images validate :image_count_in_bounds, :on =&gt; :create protected def image_count_in_bounds return if trip_images.blank? errors.add("Only 10 images are allowed!") if trip_images.length &gt; 10 end end </code></pre> <p>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):</p> <pre><code>jQuery -&gt; $('#file_field_id').change () -&gt; #disable the form for file in this.files #check each file #enable the form </code></pre> <h1>Summary</h1> <p>You can build a lot out of existing tutorials, as dragonfly does not behave that differently to other solutions when it comes to <em>just</em> to uploading files. However, if you'd like something fancier, I'd suggest <a href="https://github.com/blueimp/jQuery-File-Upload/" rel="nofollow noreferrer">jQuery Fileupload</a>, as many others have before me.</p> <p>Anyways, I hope I could provide some insight.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload