title: upload multiple files through controller using paperclip and rails
body: <p>I have used the following tutorials: </p>
  
  <p><a href="http://patshaughnessy.net/2009/5/16/paperclip-sample-app-part-2-downloading-files-through-a-controller" rel="nofollow">http://patshaughnessy.net/2009/5/16/paperclip-sample-app-part-2-downloading-files-through-a-controller</a></p>
  
  <p>This tutorial walks you through downloading files through the controller rather than as static files.  I did this to control access to the files.</p>
  
  <p><a href="http://emersonlackey.com/article/paperclip-with-rails-3" rel="nofollow">http://emersonlackey.com/article/paperclip-with-rails-3</a></p>
  
  <p>This tutorial walks you through the process of creating a model for your files and using that to allow multiple file uploads</p>
  
  <p>So, for the most part I got the uploading to work fine, however, the problem is in the downloading.<br>
  A little background.  I have the following key models.</p>
  
  <pre><code>class VisualSubmission &lt; ActiveRecord::Base
    has_many   :assets
    accepts_nested_attributes_for :assets, :allow_destroy =&gt; true;
  end
  
  class Asset &lt; ActiveRecord::Base
    belongs_to :visual_submission
  
    has_attached_file :image, :styles =&gt; { :original =&gt; "100%", :large =&gt; "600x600&gt;", :small =&gt; "150x150&gt;", :thumb =&gt; "50x50&gt;"}, :convert_options =&gt; {:all =&gt; "-auto-orient"},
                    :path =&gt; ':rails_root/secure/system/:attachment/:id/:style/:basename.:extension',
                  :url =&gt; '/:class/:id/:attachment?style=:style'
  
  end
  </code></pre>
  
  <p>In order to download files through the controller, an action is created as such:  </p>
  
  <pre><code>def images
    visual_submission = VisualSubmission.find(params[:id])
    style = params[:style] ? params[:style] : 'original'
    send_file visual_submission.image.path(style), :type =&gt;  visual_submission.image_content_type, :disposition =&gt; 'inline'   
  end
  </code></pre>
  
  <p>So, as I said above, uploading is just fine.  One thing that is different but expected, when it stores the file it uses the ID from the assets model.  This is all well and good, however, I am having trouble getting the URL correct now.  I created a route to my images: </p>
  
  <pre><code>resources :visual_submissions do
    member do
     get :images
   end
  end
  </code></pre>
  
  <p>And this is what my route looks like.</p>
  
  <p>images_visual_submission GET    /visual_submissions/:id/images(.:format)  {:action=>"images", :controller=>"visual_submissions"}</p>
  
  <p>Now the piece of code that in theory is supposed to access the image.</p>
  
  <p>This is from my edit form.  It is supposed to show the current images stored.</p>
  
  <pre><code>&lt;%= f.fields_for :assets do |asset_fields| %&gt;
    &lt;% unless asset_fields.object.new_record? %&gt;
      &lt;p&gt;
          &lt;%= asset_fields.object.image.url %&gt;
              &lt;/p&gt;
    &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Now this is obviously not going to work.  I am not sure what object is doing here, but what I do know is, my images should be going through my visual_submissions controller which this is not touching.  I am not entirely sure if I am asking this questions correctly, but I am stuck.  One idea I had was to create a assets controller and move the images method into there but I am not sure how much that will help.</p>
  
  <p>Any thoughts?
  Thanks!</p>