thojanssens1

thojanssens1

Boolean parameters in routes

In the docs for Phoenix Routing we can see several examples such as below, where a parameter is a boolean.

Routes.user_path(Endpoint, :show, 17, admin: true, active: false)
"/users/17?admin=true&active=false"

Do we ever want to pass a boolean though? Because that will result in a string “true” and “false” in the URL, and in the controller action, we’ll have to convert “true” and “false” string respectively to true and false.

I was thinking to pass 1 or 0 like I used to do in PHP, to easily convert the parameter into a boolean, but converting 0 to a boolean gives true.

iex> !!0
true

Actually, while I’m writing this, I have the idea to use String.to_existing_atom/1

String.to_existing_atom("false")
String.to_existing_atom("true")

Do you do the same? Would maybe be interesting to have a plug that detects true and false strings for any route param (and query string param) and convert them automatically to the atoms? Systematically for all incoming requests.

Marked As Solved

dimitarvp

dimitarvp

I never use the strings “true” and “false” in these situations. But even if I did, you still have to convert anything coming to your controllers from string to the proper type – and also validate! – before acting on it. Doesn’t matter much if it’s the true/false or 1/0.

This is for you to decide but I wouldn’t. Make a Plug, sure, but use it selectively inside your controllers like so:

plug BooleanParameter, only: [:admin, :use_utc_time]

The above is used with crushing success and is practically one of the reasons for Phoenix being as good as it is (although there are many reasons).

Where Next?

Popular in Questions Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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
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