iv-mexx
I’m getting started with LiveView Upload by following Chris McCord’s Video ( https://youtu.be/PffpT2eslH8?t=795 ) and I have some trouble with updating an entity with a new Image.
In the Video, in the :edit Action of the LiveView, he’s doing it like this:
post = put_photo_urls(socket, socket.assigns.post)
Timeline.update_post(post, post_params)
The update_post function in the context looks like this:
def update_post(%Post{} = post, attrs) do
post
|> Post.changeset(attrs)
|> Repo.update()
end
In the Schema he adds the :photo_urls field, but does not add it to the changeset function and says “we’ll set it directly”.
The Problem is, if I try this in my own app with a very similar structure (I just have one image_url instead of multiple photo_urls and my model is called Character with some other fields as well), the image_url does not get updated…
If I try by hand and change the image_url of an existing Character
c = Characters.get_character!(1) |> Map.put(:image_url, "some_image_url")
And then pipe it through the Changeset with an empty dictionary (note, just like in the Video, my changeset function does not include image_url)
c |> Character.changeset(%{})
I get an empty changeset back, and then piping the result into Repo.update() does not cause a Database Query.
Interestingly though, the return value of Repo.update is the character WITH image_url set…
On the other hand, it works when creating a new character and that uses the same approach (basically %Character{image_url: "some_url"} |> Character.changeset(attrs) |> Repo.insert() )
Trending in Questions
Other Trending Topics
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
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
mcrumm
Welcome @iv-mexx!
What happens if you put the new image URL into the changeset instead of into the struct? You can do so with
Ecto.Changeset.put_change/3:iv-mexx
Thanks for your quick reply!
I can kind of get it to work, but in the whole context only ugly and around many corners, and then it would certainly be better to put it into the
attrsby hand.So in the Video, Chris creates a function in the LiveView (I’m again showing my code thats basically copied but with my models)
And uses that one in both, create and edit:
If I do that and use
put_change/3in the Context, it does not change anything. I guess, because thecharacteralready has the sameimage_urlset, so it is not a change.But thats where I can’t wrap my head around, because thats exactly what Chris is doing in the Video…
To be fair, he did not demo editing, so maybe it would not work in his code as well?