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
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
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
If anyone is trying to do this you have two solutions:
- In controller use
current_url(conn) - in views because
Mockapp.Web.Router.Helpersis imported in YourApp.ex you ca do this in your template:
<%= url(@conn) %>
dsissitka
Here you go:
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








