How can I make a file upload field be consistent during edits with Elixir/Phoenix?

Viewed 153

In my form template I have this snippet:

  <%= label f, :event_banner_image %>
  <%= file_input f, :event_banner_image %>
  <%= error_tag f, :event_banner_image %>

Creating a new event (from "/events/new") with one of these images works fine, but then when I go to edit the event the file input is empty. In a way, of course it is because the server doesn't know about the local path to the file that was uploaded. Even so, I wonder if I'm missing out on a way to make this a better experience.

1 Answers

Working with images is quite tricky. How I went about such a case is by manually extracting the image path from the %Phoenix.HTML.Form{} struct, the f in your code.

<%= img_tag f.data.banner_image_path, class: ""  %>

It is much easier if it's just one image such that reuploading replaces the previous one. You can find more about the structure of phoenix HTML form here

Related