Hello Guys! i am new to the framekwork, i tried following the tutorial posted by Chris McCord in his Youtube Channel, and everything works except for the update, there was a topic similar to this posted before, and he (Chris) recommended to use
Changeset.put_change
to be able to put the changes into the database, i cant make it work
This method updates adds the photos urls,
defp save_service(socket, :edit, service_params) do
service = put_photo_urls(socket, socket.assigns.service, "SCHEMA")
urls = put_photo_urls(socket, socket.assigns.service, "")
service_params = Map.put(service_params, "photo_urls", urls)
case Timeline.update_service(service, service_params, urls, &consume_photos(socket, &1)) do
{:ok, _service} ->
{:noreply,
socket
|> put_flash(:info, "Service updated successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
this method retrieves the urls or the complete schema
defp put_photo_urls(socket, %Service{} = service, type) do
{completed, []} = uploaded_entries(socket, :photo)
urls =
for entry <- completed do
Routes.static_path(socket, "/uploads/#{entry.uuid}.#{ext(entry)}")
end
if type == "SCHEMA" do
%Service{service | photo_urls: urls}
else
urls
end
end
this method updates the timeline
def update_service(%Service{} = service, attrs, urls, after_save \\ &{:ok, &1}) do
service
|> Service.changeset(attrs)
|> Ecto.Changeset.put_change(:photo_urls, urls)
|> Repo.update()
|> after_save(after_save)
|> broadcast(:service_updated)
end
this is the schema
defmodule Buscaxalapa.Timeline.Service do
use Ecto.Schema
import Ecto.Changeset
schema "services" do
field :name, :string
field :phone, :string
field :hasdelivery, :boolean
field :averagecost, :integer
field :tag, :string
field :secondarytags, :string
field :servicedesc, :string
field :address, :string
field :franchise, :string
field :pictures, :string
field :lat, :string
field :long, :string
field :photo_urls, {:array, :string}, default: []
timestamps()
end
@doc false
def changeset(service, attrs) do
service
|> cast(attrs, [:photo_urls, :name, :phone, :hasdelivery, :averagecost, :tag, :secondarytags, :servicedesc, :address, :franchise, :pictures, :lat, :long])
|> validate_required([:name, :phone, :hasdelivery, :averagecost, :tag, :secondarytags, :servicedesc, :address, :franchise, :pictures, :lat, :long])
end
end
the HTML component works, because it updates when i create a new service. Any ideas what could be wrong with updating the photo urls? it works if i try to update any other field but the photo_urls. Also, it does not show me any errors, but the db does not reflect the changes