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