RisingFromAshes

RisingFromAshes

How to get the current URL?

I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just finding my way around the docs eg not even sure where to find info on conn opt.

Thanks

Marked As Solved

josevalim

josevalim

Creator of Elixir

The simplest way is to get the normalized path in your view:

"/" <> Enum.join(@conn.path_info, "/")

The simplest way is to get the request path, which is the path as typed in the browser, not normalized (for example, it may have double slashes - and I am not talking about the guitarist) in your view:

@conn.request_path

And if you want the url from those, you can use the url/1 function:

url(@conn, "/" <> Enum.join(@conn.path_info, "/"))
url(@conn, @conn.request_path)

Also Liked

dsissitka

dsissitka

What would you like to do with it?

In a controller it’s accessible via conn.request_path. In a template it’s accessible via @conn.request_path.

Here’s a good example:

http://stackoverflow.com/questions/31572653/apply-css-class-on-element-if-current-page-in-phoenix

It adds class="active" to links that point to the current page.

alexandrubagu

alexandrubagu

If anyone is trying to do this you have two solutions:

  1. In controller use current_url(conn)
  2. in views because Mockapp.Web.Router.Helpers is imported in YourApp.ex you ca do this in your template:
    <%= url(@conn) %>

Last Post!

uzairaslam196

uzairaslam196

Thank You @LostKobrakai, wondering why I couldn’t see this method.

Where Next?

Popular in Questions Top

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
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement