thiagomajesk

thiagomajesk

Default (optional) param value in route definition

Hi everyone! I’m trying to find a way to add a default value on a route param in Phoenix. I’ve searched in the forum but couldn’t find an exact match to what I’m seeking.

Example: Let’s suppose there’s a page that shows logs that can be filtered. A user could click an option to filter logs from 7 days up to 30 days and we’d have routes like:

get "/logs", LogController, :index`
  • /logs?days=7
  • /logs?days=30

This is very trivial to achieve because any query parameter is matched against this route, but what if I want to make the days param optional? In other words, filling the value if the user does not specify one.

I know we could simply do that by hand merging the conn.params values with a default map after the action was already matched, but the problem with this approach is that the route won’t change to represent the current page state. If I want to always filter logs that are 1 day old by default, when the user accesses the /logs page, it would have to automatically fill the params as /logs?days=1.

I don’t work with Rails, but it seems that it has similar functionality to other frameworks that I’ve used before: Rails Routing from the Outside In — Ruby on Rails Guides.
So, I was wondering if Phoenix has something like this built-in or its necessary to create a custom plug.

Most Liked

fuelen

fuelen

This can be resolved by simple plug, which redirects to /logs?days=1 if days parameter is empty

NobbZ

NobbZ

Don’t match on days, but do something like this in your action:

days = Map.get(params, "days", 1)
NobbZ

NobbZ

Yes of course.

The last option you have involves JavaScript and push state to use that to add the parameters to the URL on the browsers end, if it does support to do so…

Just using defaults, without having them explicitly in the URL is the easiest solution, does not involve additional round trips or unreliabilities Like Javascript.

Then we have the redirect variants which are reliable, but do have the additional roundtrips.

Last but not least, there is JavaScript and push state, which from the server side just works as already explained for just assuming defaults, but also serve a JavaScript that will normalize the URLs via push state. This will not cause redirects, is reliable to have proper values on the server, but is not reliably showing them on the client.

Last Post!

thiagomajesk

thiagomajesk

Hi, @manicar2093, welcome to the forums! If you are using LiveView that’s a trick you can use. There’s no way of doing that from the route definition though. Cheers!

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
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