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.