I'm using a form partial to both create and edit a model object that contains an uploader ("file"):
= simple_form_for(@document) do |f|
= f.error_notification
.form-inputs
= f.input :event_id
= f.input :name
= f.input :file
= f.hidden_field :file_cache
.form-actions
= f.button :submit
In the edit form rather than getting the filename associated with the already uploaded file I get "No file chosen". is there a way to get the widget to recognize that the uploader is not nil and use the filename from the uploader?
You cant for security reasons manually set the value in a file input field. The file name reflects the file chosen by the browser but the file has already been uploaded at that point thus why @document.file? would show true if you look. The file_cache is only used to store and resend the attributes already sent not to be confused with overriding the input value of a file field. For example if you changed the hidden field to an input field you would see the same "no file" message.
I realize this is not what you want, but maybe a better way to handel this is to relabel the input to say something like "replace file" and then render out the file nave via
@document.file if @document.file?
above the field.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With