Reviewing Phoenix LiveView Uploads Deep Dive

First I want to say I’m about to outline an issue with the video @chrismccord did, that said it’s still great he even created the video in the first place and I’m very grateful for his and everyone’s efforts. I realize it’s a learning experience for everyone and this is just a part of that process. So again thank you to everyone who contributes to this community and software. I feel I don’t say that enough.

Ok, so the issues:

There looks to be one critical issue with the design pattern used for setting the urls.
Using the put_image_urls for the edit action won’t persist the db. This is a tricky one because ecto does not complain and the return value from calling Repo.update even shows the correct data. The problem is the data never gets persisted to the db on update.

Take this for example. Note: Repo.update()

iex(1)> {:ok, %{id: id}} = %Post{ p | image_urls: ['foo']} 
|> Post.changeset(%{}) 
|> Repo.update()

{:ok,
 %Post{
   id: 2,
   image_urls: ['foo'],
   ...
 }}

iex(2)>  Post |> Repo.get(id)
%Post{
  id: 2,
  image_urls: [],
  ...
}

For Repo.insert() the above would have persisted but not for update.

This leads me to the question,
Why are we not building up the post_params rather than directly on the post?
Maybe just an oversight?

I get this is outside the context of the example video showcasing how to do something with liveview, though I’m left wondering if I am overthinking how we pass in extra params like this and general design choices like this.

1 Like

Yes. While live coding it I only considered creating a new post :slight_smile:

You can use Changeset.put_change and should be good to go!

4 Likes

As to the question around just building up a new post_params in save_post for edit.

Do you see any issue with having put_image_urls returning only the urls then building up the post_params before calling Blog.create_post(post, post_params, &consume_images(socket, &1)) ?

I guess I’m trying to understand why that idea was not the inherent way vs having put_image_urls return a post.

1 Like

@chrismccord I wonder what broadcast/2 does in the video (somewhere in create and update post functions) do you have the sample source code? I am currently having a problem with liveview upload. In your example, if creating or updating a post takes time then handle_event will timeout so I am thinking of a way how to make it asynchronous and just broadcast an event after creating or updating.