When upload pictures, everything goes okay. But on edit, it doesn't display the file fields value. Just an empty file_field, like nothing is there. Pic title displays correctly. Other text is in hungarian.
_form.html.haml
= simple_nested_form_for(@post) do |f|
= f.input :title, label: 'Cím'
= f.input :body, label: "Test"
= f.fields_for :pics do |pic_form|
= pic_form.text_field :title
%br/
= pic_form.file_field :image
= pic_form.link_to_remove "Kép Törlése", class: "btn btn-warning"
%br/
%br/
%p= f.link_to_add "Kép hozzáadása", :pics, class: "btn btn-success"
%br/
%br/
= f.submit "Mentés", class: "btn btn-primary"
= javascript_include_tag :defaults, "nested_form"
How to pass, the existing file to the file_field?
You have to use a conditional to find out if the file(s) is/are attached. Using ActiveStorage, the conditional could look like this (for a field that accepts only one file):
<% if @my_object.my_file.attached? %>
<%= @my_object.my_file.blob.filename %>
<% else %>
<%= f.file_field :my_file %>
<% end %>
If the field accepts many files, you have to iterate to show the filenames:
<% @my_object.my_files.each do |i| %>
<%= i.filename %><br>
<% end %>
I am sure there is a corresponding way to handle this in Carrierwave.
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