Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep file field value when validation failed

I have a classic rails 3 form with a file field, everything works fine, upload works and data is saved to database.

When a validation failed, for example, title is missing, then the user is sent back to the form with a render :action => new. Normal. But the problem here, is that the user have to select another time its file.

Any way to avoid that?

like image 582
Hartator Avatar asked Feb 02 '26 20:02

Hartator


2 Answers

Typically you don't want to process files until after validations have run or you're going to repeatedly store files that possibly don't have the associated records. Gems like Paperclip and attachment_fu do this.

If you would rather store the file the first time it's submitted and is valid you can store the file and then do a quick check in your view to see if it's already set for the object you're building a form for, e.g:

<% unless foo.attachment? %>
  # file field
<% end %>

Make sense?

like image 50
coreyward Avatar answered Feb 04 '26 09:02

coreyward


You can't. It is security issue.

You can hack it in some browsers, but generally you can't do it

like image 43
fl00r Avatar answered Feb 04 '26 09:02

fl00r



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!