Redirect to a url with anchor or url variable

Hi, please I can’t seem to get my around adding an anchor tag to a url

Fo instance, i have a url http://localhost:4000/reviews/view/id_of_review.

But i want to be able to do something like this in some cases

http://localhost:4000/reviews/view/id_of_review#comments

Please how can I get this done

1 Like

I don’t think that the Phoenix path helpers will let you define anchors in order to generate the final path, just parameters, so maybe a quick and simple (although not quite elegant) solution could be to create the links with something like this:

<%= link("Some text", to: "#{your_helper_path(@conn, :show, ELEMENT_ID, param1: param1, param2: param2, ...)}#your_anchor", class: "...") %>

Thanks @dreamingechoes
I need it inside my controller. I have something similar to the code below

conn |> redirect(to: review_path(conn, :view, review.slug, review.id))

And i want to be able to add the anchor in the the redirect.

Is it possible? Or how can i achieve this.

Thanks @dreamingechoes, i was able to adapt your code to my use case and it worked pretty well. Thanks immensely. I appreciate!

God bless you in Jesus name, Amen.

1 Like

For anyone else:

conn |> redirect(to: "#{review_path(conn, :view, review.slug, review.id)}#blahanchor")

Or:

conn |> redirect(to: review_path(conn, :view, review.slug, review.id)<>"#blahanchor")
4 Likes

Apologies for the late response @papakay, I was busy at work :pensive: And thank you very much @OvermindDL1, that’s exactly what I was going to reply :blush::pray:

2 Likes

Remember though, that using a fragment in an absolute URI is not allowed per RFC 3986.

So your clients behaviour is unspecified.

1 Like

If you wanna be more explicit about what you’re doing you can use URI.

conn |> redirect(to: conn |> review_path(:view, review.slug, review.id) |> URI.parse() |> Map.put(:fragment, "blahanchor") |> URI.to_string())
1 Like