How to use fragment identifiers in path helpers in phoenix webframework?

275 Views Asked by At

Is there a way to add a fragment identifier to a path helper?

I try to link back from my login page to the about section in the landing page.

<a href="localhost:4000/#about">ABOUT</a>

Using this path helper, I only get back to the landing page:

<li><a href="<%= page_path(@conn, :index) %>">ABOUT</a></li>

But I would like to get to the about section at the following path:

localhost:4000/#about

I tried to combine the path, but without success:

<li><a href="<%= page_path(@conn, :index) <> "#about" %>">ABOUT</a></li>

Thank you in advanced for any help!

2

There are 2 best solutions below

1
Aleksei Matiushkin On

One cannot nest the double quotes:

<a href="<%= page_path(@conn, :index) <> "#about" %>"

the above obviously results in an error, because it reads as shown below, strings are denoted:

#       ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓      ⇓⇓⇓⇓⇓
<a href="<%= page_path(@conn, :index) <> "#about" %>"

To make is work, use the ~s sigil:

<a href=~s|<%= page_path(@conn, :index) <> "/#about" %>|
3
Abdullah Esmail On

You can just move the #about string out of the interpolated expression:

<li><a href="<%= page_path(@conn, :index) %>#about">ABOUT</a></li>