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
Trending in Questions
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
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
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
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
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
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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.
ppetrov
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
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
On the off chance you will be uploading to
priv/staticin production, use:code.priv_dir(:my_app)(:my_appbeing the name of your app) as thepriv/is in a different location in production.This could be viewed as nit-picky but I always pattern match (note singular
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.