Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying file field value on edit

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.

enter image description here

_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?

like image 791
Barna Kovacs Avatar asked Apr 30 '13 08:04

Barna Kovacs


1 Answers

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.

like image 176
Jussi Hirvi Avatar answered Sep 25 '22 04:09

Jussi Hirvi