Again, we’re trying to make this the most understandable code, without any regard whatsoever for elegance. In the last post we made a do-nothing uploader, just to get some feel for the whole HTML5 file upload process.
This time, we’re actually going to upload, attach, and display files using Paperclip and Rails. Since I am doing this on company time, I’m going to use a new app that I need to get done anyway.
The Summit County Engineer keeps track of Road Records, which are mostly scanned drawings of survey plats for new roads, expanded roads, etc. There’s a bunch of fields about which road, which section, what kind of work, etc. These, you can ignore. The important part is that a Road Record can have a bunch of attached files of varying formats, e.g. tif, pdf, jpg, and doc.
For this tutorial (and my app,) I’m going to call them Appended Assets.
For the most part, it’s typical Paperclip. (one caveat: Paperclip 2.3.14 has a bug — fixed in Paperclip 2.3.15) Most of the non-typical work gets done in the road_records_controller, so let’s look at that first.
It just adds a new AppendedAsset for every file it gets. The fact that the file gets stored in /tmp/ for a bit gives you the chance to rename it, sanitize it, or whatever before it actually gets attached. Don’t forget to add the route:
resources :road_records do
The RoadRecord model is simplicity itself.
And the AppendedAsset model is nearly as simple.
Notice that you can do all the usual Paperclip magic, just as you’re used to.
All that’s left is the upload part, which is very similar to what was shown in the last post:
Finally, there is the show.html.erb file, which demonstrates that all the Paperclippy goodness is still there.
It doesn’t look like all that much, but I scrounged all over the web for bits of this. Thanks to all those who post demos and tuts, I guess.