polypush135

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

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

josevalim

Creator of Elixir

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 :slight_smile:

sodapopcan

sodapopcan

I believe you can do:

foo = ~w[foo bar]
dbg ~p"/#{foo}"
polypush135

polypush135

Thank you :pray: that did it.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement