I am building a custom image upload form in Rails 3 (paperclip / carrierwave is not an option for our setup) and need to do the classic “persist image on validation fail” scenario. What is the “best practice” way to do this in Rails 3?
Summary:
- User fills out form with image
- User click “send form”
- Form validation fails
- User should now see the image they uploaded in the form and not need to select it again
If you are doing it from scratch, you have to put the file into the uploads directory (or create a new directory inside it to store different versions), and store the temporary file name into one not-persisted field of your model.
For example, that not-persisted field could be named photo_cache_dir:
In the template you just check for that field to be present:
… and in that case, using your own logic, display the uploaded photo.
I don’t know if this is the best practice, but it’s what I’m using and it works very well.