ashrafhasson

ashrafhasson

Verified routes ~p sigil replaces / with %2F for route with or without params

Hello everyone,

I’m using ~p in one of my liveviews to push_patch based on an event and the resulting path from the snippet below appears to be messed up where the /s are replaced with %2Fs regardless of whether I include query params or not. I’m not sure where I’m going wrong here, any help please?

def handle_info({:update, opts}, socket) do
    params = PaginationForm.merge_and_sanitize_params(socket, opts)
    current_path = socket.assigns.current_path
    |> String.trim_leading("/")
    path = ~p"/#{current_path}"
    {:noreply, push_patch(socket, to: path, replace: true)}
end

affixing the query params with path = ~p"/#{current_path}?#{params}" has no effect. The resulting path/error is like so:

** (ArgumentError) cannot push_patch/2 to %URI{scheme: "http", 
  userinfo: nil, 
  host: "localhost", 
  port: 4000, 
  path: "/en%2Fitems%2F1%2Fstatement", 
  query:  query: "page=1&page_size=10", 
  fragment: nil} 
because the given path does not point to the current root view
...

The relevant router path is defined in a locale scope:

  scope "/:locale", MyAppWeb do
    pipe_through [:browser, :localization, :require_authenticated_user]

    live "/items/:id/statement", ItemLive.Show, :statement

The app uses Phoenix 1.7.11 (recent upgrade)

Thanks in advance!

Marked As Solved

sodapopcan

sodapopcan

You can only use interpolation with constructs that implement the Phoenix.Param protocol. You are passing it a string which happens to contain /s but it’s just interpreting it has a single path fragment. There isn’t much point in wrapping a current_path in ~p as it should already be verified up the call stack that it’s a valid path.

Also Liked

LostKobrakai

LostKobrakai

Verified routes allow interpolation only for individual path segments, because that’s the only way they can deal with dynamic parts of a path without loosing all capability of verifying anything about the path. Hence all / are escaped, because the expectation is that you just pass a single path segment.

For ~p"/#{current_path}" you can just as well not use sigil p to begin with. It couldn’t validate anything about current_path anyways, given nothing about it is known at compile time.

ashrafhasson

ashrafhasson

@sodapopcan @LostKobrakai both of what you said makes sense, thanks for explaining the basics, I would have tagged both answers as acceptable solution but that’s not possible :slight_smile:
I was hoping that I could interpolate the whole thing to get the query params piece automatically taken care of, converting the map to a query string. Guess I’ll URI.merge the two parts then :sweat_smile:

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement