Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

carrierwave in an edit form

Tags:

carrierwave

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?

like image 956
Mike Summers Avatar asked Oct 21 '22 19:10

Mike Summers


1 Answers

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.

like image 105
Polygon Pusher Avatar answered Oct 25 '22 19:10

Polygon Pusher