Does anybody have a Rails 3 example of multiple attachments working with validation on a multipart form? I've been trying to get this working forever (and have found every blog post and message I could, but none cover this situation, and the docs don't help at all).
The first problem is that most examples use 'new_record?' in the view template, but this always returns true in a new/create sequence when validation fails because no model instances have been saved (so no 'id' value). So if you start with 5 model instances/file inputs and upload one file, you now have 6 file inputs presented when you re-render the new view, and the 'unless' clause fails for the same reason and no thumbnails are presented.
I want to preserve the link to the uploaded file (and I know this is possible--they're living in a temp directory) while presenting validation errors to the user for other required fields.
Somebody somewhere must have this working with Paperclip. ;)
You could achieve this by using three models, and a bit of controller magic.
The first model is the model you actually want. Let's say Biography.
Now in your controller you can do something like this:
The way I'm using:
I have properties that has many photos (10 in case). Going to the code whe have:
In properties controller:
10.times "render" 10 times the photo field in view. When in edit form, just apper photo fields lefting. For exemple: At 1st time I uploaded 3 photos, then if I want to upload more, just 7 fields appear.
In property model I have:
This allow photos to be uploaded.
Photo model:
In my form partial:
The answer I went with is to use carrierwave instead of paperclip. It only took me about an hour to cut over, and it solves this problem out of the box.
Here's my answer in another thread with much more detail: Not losing paperclip attachment when model cannot be saved due to validation error