defp save_post(socket, :edit, post_params) do
post = put_photo_urls(socket, socket.assigns.post)
post_params = Map.put_new(post_params, "photo_urls", post.photo_urls)
case Timeline.update_post(post, post_params, &consume_photos(socket, &1)) do
{:ok, _post} ->
{:noreply,
socket
|> put_flash(:info, "Post updated successfully")
|> push_redirect(to: socket.assigns.return_to)}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, :changeset, changeset)}
end
end
Why there is no change at all unless I manually type the path URL instead of just letting post.photo_urls that is a list?
``post_params = Map.put_new(post_params, “photo_urls”, [“uploads/uuid.png”])`
Hi @boilercoding welcome!
In order to help, we need to see your Post schema. We also need to see what the IO.inspect(post_params)
output is so that we can see what information is coming in.
defmodule LiveviewPlayground.Timeline.Post do
use Ecto.Schema
import Ecto.Changeset
schema "posts" do
field :body, :string
field :likes_count, :integer, default: 0
field :reposts_count, :integer, default: 0
field :username, :string, default: "Anthony"
field :photo_urls, {:array, :string}, default: []
timestamps()
end
@doc false
def changeset(post, attrs) do
post
|> cast(attrs, [:body, :photo_urls])
|> validate_required([:body])
|> validate_length(:body, min: 2, max: 250)
end
end
IO.inspect(post_params)
%{"body" => "sent from form form"}
Silly me!!! I just needed to send the old post to the update function instead of sending the new one.
case Timeline.update_post(socket.assigns.post, post_params, &consume_photos(socket, &1))