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

Tee
can someone please explain to me how Enum.reduce works with maps
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
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42842 311
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35953 110
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement