polypush135
Phoenix Verified routes and escaping URI
Trying to do something like.
some_route = "foo/bar"
~p"#{some_route}"
Aside from the fact the compile checker complains, the / in between foo and bar gets escaped inside the ~p somewhere so the request ends up crying that foo%2Fbar can be found.
I realize this has everything to do with escapable chars in my strings.
My question is: what is the best way to go about something like this?
I ran into this issue as a result of generators and the patch assignment on the Live component for the FormComponent.
The issue was, I wanted to use a slug as a @derived param for my resource. But the patch=~p/ does not account for the update to the slug on the post in question when pushing the patch. For example.
defp save_post(socket, :edit, post_params) do
case Blog.update_post(socket.assigns.post, post_params) do
{:ok, post} ->
{:noreply,
socket
|> put_flash(:info, "Post updated successfully")
|> push_patch(to: ~p"/#{socket.assigns.patch}")}
...
end
end
This has no way of pulling out the new slug from the updated post value. Thus it tried to push patch to the prior value. The idea I had was to break apart the patch assignments. Something like.
defp save_post(socket, :edit, post_params) do
case Blog.update_post(socket.assigns.post, post_params) do
{:ok, post} ->
notify_parent({:saved, post})
{:noreply,
socket
|> put_flash(:info, "Post updated successfully")
|> push_patch(to: ~p"/#{socket.assigns.patch}/#{post.slug}")}
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign_form(socket, changeset)}
end
end
note: p"/#{socket.assigns.patch}/#{post.slug}")
And at the time of assignment.
<.live_component
module={MorphicProWeb.Admin.PostLive.FormComponent}
id={@post.id}
title={@page_title}
action={@live_action}
post={@post}
patch={"admin/posts")}
/>
Thus admin/posts turns into admin&2Fposts and fails. Also note I could not get the protocol to_params to work on the post thus I’m passing it explicitly the slug via. post.slug so there’s that too.
Any input would be great, thanks.
Marked As Solved
polypush135
I got it, I should pass a lambda instead of assigning a rendered value to the patch assigns. Then call into that lambda at run time. I bet it has to do with the way the call to ~p is composed. This way I can pass the ~p"/admin/posts" correctly at the time I define ~p. Also remember the warning is at compile time. So there is no other LV competing with it at that time. I think its just the super dynamic way that ~w get assigned to the socket.assigns that makes it hard for it to match in the router.
patch={fn %{slug: slug} -> ~p"/admin/posts/#{slug}" end}
and
socket.assigns.patch.(post)
respectfully did address the warnings and provided the logic I was looking for.
Thanks for the help @sodapopcan
Also Liked
josevalim
Correct. The point of verified routes is to verify the routes at compile time, which it cannot in your case. In your case, you can build the route yourself, as a string without the sigil, since verified routes is not giving you anything, or be less dynamic and pass the route ~p as parameter ![]()
sodapopcan
I believe you can do:
foo = ~w[foo bar]
dbg ~p"/#{foo}"
polypush135
Thank you
that did it.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance










