Need help with live image upload - doesn't save to DB

Hello, newbie here.

I am trying to implement live file upload. So far i got to the point i can upload the file and get the params correctly. However it does not save the params to the DB so i can’t really display the data. For some reason after uploading the files just before the save part it reloads and does not go to saving the data to DB.

This is my render function:

def render(assigns) do
    ~H"""

      <div class="p-10 md:p-[100px]">



            <.simple_form for={@changeset} id="nomenee-form" phx-change="validate" phx-submit="save" >
            <div class="font-bold text-center mb-20 w-full font-comfortaa text-purple-900 text-2xl"> Номинация в област "Любим университетски преподавател в област Логопедия"</div>

                <%= inspect(@uploads.image) %>
              <.input field={@form[:name]} type="text" label="Име"  />

                <.input field={@form[:description]} type="textarea" label="Описание" />
                <div class="container" phx-drop-target={@uploads.image.ref}>
                <.live_file_input upload={@uploads.image} />
                </div>

                <div :for={entry <- @uploads.image.entries} class="entry">
                <.live_img_preview entry={entry} />

                <div class="progress">
                  <.error :for={err <- upload_errors(@uploads.image, entry)}>
                    <%= Phoenix.Naming.humanize(err) %>
                  </.error>
                  </div>
                  </div>
                <.live_select field={@form[:category_id]} placeholder="Категория"  />
                <.input field={@form[:published]} type="checkbox" label="Публикуван" phx-update="ignore" />



                <:actions>

                <.button phx-disable-with="Изпращане...">Изпрати номинацията</.button>

                </:actions>

            </.simple_form>

      </div>


                <%= # inspect @townoptions, pretty: true %>

    """

  end

This is my schema:

defmodule Logonag.Nomenees.Nomenee do
  use Ecto.Schema
  import Ecto.Changeset

  schema "nomenees" do
    field :name, :string
    field :description, :string
    field :image, :string
    field :published, :boolean
    field :category_id, :id

    timestamps()
  end

  @doc false
  def changeset(nomenee, attrs) do
    nomenee
    |> cast(attrs, [:name, :description, :published, :category_id, :image])
    |> validate_required([:name, :description, :published, :category_id, :image])
  end
end

This is my save function:


def handle_event("save", %{"nomenee" => nomenee_params}, socket) do
  
  photo_locations =
    consume_uploaded_entries(socket, :image, fn meta, entry ->
      dest =
        Path.join([
          "priv",
          "static",
          "uploads",
          "#{entry.uuid}-#{entry.client_name}"
        ])

      File.cp!(meta.path, dest)

      url_path = static_path(socket, "/uploads/#{Path.basename(dest)}")

      {:ok, url_path}
    end)

    nomenee_params = Map.put(nomenee_params, "image", photo_locations)

    IO.inspect(nomenee_params, label: "nomenee_params------------")
  case Nomenees.create_nomenee(nomenee_params) do
    {:ok, _nomenee} ->
      changeset = Nomenees.change_nomenee(%Nomenee{})
      {:noreply, assign(socket, :form, to_form(changeset))}
    {:error, %Ecto.Changeset{} = changeset} ->
      {:noreply, assign(socket, :form, to_form(changeset))}
      end
end

From my log i have the inspect of the nomenee_params and then it re-mounts and never pushes the changes to db:

nomenee_params------------: %{
  "category_id" => "2",
  "category_id_text_input" => "cat 2",
  "description" => "sadf",
  "image" => ["/uploads/b3d164ae-cae7-4c89-9c49-e02424037e6d-gergana-markova.jpg"],
  "name" => "asdf",
  "published" => "true"
}
[debug] Replied in 5ms
[debug] Live reload: priv/static/uploads/b3d164ae-cae7-4c89-9c49-e02424037e6d-gergana-markova.jpg
[debug] Live reload: priv/static/uploads/b3d164ae-cae7-4c89-9c49-e02424037e6d-gergana-markova.jpg
[debug] Live reload: priv/static/uploads/b3d164ae-cae7-4c89-9c49-e02424037e6d-gergana-markova.jpg
[debug] Live reload: priv/static/uploads/b3d164ae-cae7-4c89-9c49-e02424037e6d-gergana-markova.jpg
[debug] Live reload: priv/static/uploads/b3d164ae-cae7-4c89-9c49-e02424037e6d-gergana-markova.jpg
[debug] Live reload: priv/static/uploads/b3d164ae-cae7-4c89-9c49-e02424037e6d-gergana-markova.jpg
[debug] Live reload: priv/static/uploads/b3d164ae-cae7-4c89-9c49-e02424037e6d-gergana-markova.jpg
[debug] Live reload: priv/static/uploads/b3d164ae-cae7-4c89-9c49-e02424037e6d-gergana-markova.jpg
[debug] Live reload: priv/static/uploads/b3d164ae-cae7-4c89-9c49-e02424037e6d-gergana-markova.jpg
[debug] Live reload: priv/static/uploads/b3d164ae-cae7-4c89-9c49-e02424037e6d-gergana-markova.jpg
[info] GET /nomenees
Mounted started

I have tried to put the last bit in private function but got the same result. Please help me understand what i am missing here.

Regards,
Peter

photo_locations is a list, in your schema you want a string, since you’re uploading a single image you can just get the head of the list when passing to params.

nomenee_params = Map.put(nomenee_params, "image", hd(photo_locations))
1 Like

Thanks Thomas. Can you advise me on how to make it better? Like should i change the field type or collect the image name properly?

Not really, you’re trying to upload a single image and consume_uploaded_entries return a list of entries, you just grab the head of the list and go on.

The reason why it returns a list is that you can configure a single input to receive multiple images, but since it is a single one grabbing the head of the list is ok.

For the sake of completeness there is a consume_uploadeded_entry/3, that works with a single entry but is lower level than the one you’re using.

On the off chance you will be uploading to priv/static in production, use :code.priv_dir(:my_app) (:my_app being the name of your app) as the priv/ is in a different location in production.

This could be viewed as nit-picky but I always pattern match (note singular photo_location):

[photo_location] =
  consume_uploaded_entries(socket, :image, fn meta, entry ->
    # ...
  end

nomenee_params = Map.put(nomenee_params, "image", photo_location)

This way if something exceptional happens like someone somehow manages to upload more than one image (meaning they are probably up to no good), it will crash. It also reads more explicitly in that you are asserting there should only one entry.

3 Likes