The best way to keep uploaded photos if validation fails (Paperclip)


I’m using paperclip gem to upload photo with some other information. Lets call model “Post”. When validation fails, for example, because title can’t be empty, I want to keep uploaded file, so user don’t need to upload it again.

What is the best way to do this? I think it’s possible to store image data into hidden fields. Or, perhaps there are more elegant solution than this?


I believe the best approach is to simply not redirect when the validation fails. You can use ajax for remote validations. A more passive solution would be to use something like SimpleForm which will provide some default validations for common things such as missing required fields. A guess another workaround might be to somehow store the file temporarily on the server and keep a reference to it in your session data, but honestly it sounds like a lot of setup to me for very little gain. JavaScript would be the much lower hanging fruit.

I remember reading somewhere that the main reason file uploads are removed when you do a redirect :back for instance is a security precaution.

Thanks, Nozpheratu.

It makes sense, perhaps client-side validation can fix it.

thank you.