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

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement