Multiple File Uploads with Paperclip & Rails 3 (Screencast)

I really *really* need to update this screencast. I've learned so much over the last year that it's embarrassing to have this public. I'm leaving this up because some people have found it useful, but I'm hoping to do a proper update soon. Perhaps I'll wait for Rails 4...

Rails 3 Multiple File Uploads

Head over to Github and grab the full source of this application. More show notes will be online soon.

  1. A2a80673fd2f6d185c61defd4410f42a

    Laen

    5:37 AM on Saturday, February 19th, 2011

    Thanks! This is excellent.

  2. 74c02dbdaf622ad93214a10bc680046e

    Adam21e

    9:49 AM on Wednesday, March 9th, 2011

    Thanks for the great screencast, much appreciated. :-)

    I was wondering if you knew how to add validations to the model (eg validates :title, :presence => true) and have the attachments preserved if the title is missing so they don't need to be uploaded again.

  3. F64504e4eddbf96e60cdff88a4dde762

    David Tidwell

    8:27 AM on Thursday, March 10th, 2011

    Hi, just wanted to thank you for this. I watched your tutorial before, but it was back in rails 2, so I'm really excited to watch this in rails 3!

  4. 9f0c665fa2da29985be5f31bf19a4eca

    Chris

    8:41 AM on Thursday, April 21st, 2011

    How do you show these images in the views. When I do post.assets[0].url I get url is an undefined method. How else could you grab the url to generate an image with the image tag outside of the form as shown in the screencast?

  5. 30352c63a09d6ac86171756b242d3222

    seamusjr

    9:56 PM on Sunday, August 14th, 2011

    Great tutorial for adding multiple images to models with paperclip in rails 3. To answer Chris's question, you can do this in the show page to iterate through your image collection.


  6. 30352c63a09d6ac86171756b242d3222

    seamusjr

    10:01 PM on Sunday, August 14th, 2011

    To answer Chris's question, you can do this in the show page to iterate through your image collection:
    https://gist.github.com/1145350

  7. 802150025e2e8b524e4d5f01259738dc

    rmagnum2002

    11:10 PM on Wednesday, September 7th, 2011

    https://gist.github.com/1145350
    this isn't working..


    gave me an error

    undefined method `assets' for nil:NilClass

    Extracted source (around line #12):

    9:
    10:
    11:
    12:
    ...

    please help. thanks

  8. 802150025e2e8b524e4d5f01259738dc

    rmagnum2002

    12:46 PM on Thursday, September 8th, 2011

    how can you take one asset to the index.html?

    I mean display a thumb next to the post.
    And how can you limit the upload files? cause I can upload 5 then edit the post and upload 5 more and so on.. ending up having more images than needed

  9. C67ef9a039231500c82e6f00218946fa

    Emils

    5:09 PM on Wednesday, September 21st, 2011

    Great screencast !

    But
    When I try to upload an image, I get - ActiveRecord::UnknownAttributeError (unknown attribute: image).

    Anyone has this issue?
    Any solutions ?

    I noticed that this app from git hub is on rails 3.0.3 (My app 3.1.0) - Could this be the case ?

    Thanks!

  10. B57f81d2e20714436fa85b1c2cc19183

    Hunter Husar

    1:14 AM on Thursday, October 6th, 2011

    Thank you! Very great tutorial very helpful :) !

  11. F80c5168b92fe9d49b0668cde3bdd57c

    Gordon Yeong

    8:12 AM on Monday, October 10th, 2011

    thanks for doing this. I too did multi images uploads using rails 2.3.8 but with a new project on rails 3, your guide provided me opportunity to further simplify my code. On rails 2.3.8, i relied on js to dynamically create more file fields in the view but with yours, i just calculated the number of new file fields required when the 'update' message is sent to the resource controller :)
    Sweet :D

  12. 41e5f83acb50296ace67e8a30c551fb1

    Prashanth

    5:12 AM on Sunday, December 18th, 2011

    Great screencast, saved my day!!

    Keep them coming :)

  13. 565cf7871155cca5be71f74f3a3334a7

    Steve

    1:30 AM on Wednesday, December 28th, 2011

    Thanks for this screencast. VERY helpful. I seem to be having a problem with displaying the attachments in my show view. Can you email me the code you use in your show view? I appreciate your work on this topic.

  14. 1f17549d70c4b08844decb5f0067cdc2

    Tony F

    4:24 AM on Saturday, June 2nd, 2012

    great screencast - very helpful - thanks! :D

  15. 997ff5e87aded9863b632d3765d11265

    Yaniv

    1:15 PM on Wednesday, July 11th, 2012

    Great cast!
    My problem is, every image is duplicated upon create/update, that is, saved twice to the db (and afterwards displayed twice in the view as a result)
    In other pages, and even in the same page as well, I also have a single upload file, which works fine.
    As a rails newbie, I don't really know where to look at...
    Help appreciated.

  16. 859ef0588a333f7e701da46ef4174749

    Ariel Crippa

    8:13 PM on Friday, September 14th, 2012

    I have a problem to show the 5 fields file, I can not make them appear, I followed all the tutorial and I have only this error, that could be the problem?
    Thanks!

  17. B8f1f9f0a525f8b7b51313056db7fe23

    Yura

    7:39 AM on Monday, October 15th, 2012

    Great tut! Thanks. Is there way to update it to use one file field with html5 multiple attribute?

  18. Fd34aa3896f53f9340573299d9e73441

    Reejo

    5:52 AM on Tuesday, December 4th, 2012

    I would like to suggest few modifications.

    This is a really cool way to preload the images already attached to the post.
    "http://stackoverflow.com/questions/10266005/updating-paperclip-avatar-in-multipart-simple-form"

    then in the Controller - edit method can be
    (5-(@post.assests.count)).times { @post.assests.build }

    and in the form you can change it



    :avatar,
    label: "Image" %>