ppetrov

ppetrov

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

Showing Posts 1 to 4

thomas.fortes

thomas.fortes

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))
ppetrov

ppetrov OP

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?

thomas.fortes

thomas.fortes

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.

sodapopcan

sodapopcan

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews