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`
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
I don’t work with Rails, but it seems that it has similar functionality to other frameworks that I’ve used before: https://guides.rubyonrails.org/routing.html#defining-defaults.
So, I was wondering if Phoenix has something like this built-in or its necessary to create a custom plug.