Sheharyar Naseer

Clean Atom parameters in Phoenix

One thing I’ve always hated about working with Phoenix is the fact that I have to use strings to reference web request parameters in controllers. To any other developer, this might sound silly or even insane, but to my OCD-riddled brain, this is pure agony:

# web/controllers/some_controller.ex

def show(conn, %{"id" => id}) do
  # do something

I guess I would’ve never even thought about using Atoms for parameters if I wasn’t already used to symbols in Rails:


To solve this “problem”, I’ve been using a custom plug in my Elixir applications for the past few months, that essentially symbolizes (or “atomizes”) all keys in the params map in controllers. After some input from other Elixir developers, and major security improvements, I published it as a package on Hex, called BetterParams, allowing you to do this:

def create(conn, %{id: id, post: %{title: title, body: body}}) do
  # or this: params[:post][:title]

Implementation uses String.to_existing_atom to prevent against Erlang atom table DoS attacks, and keeps the existing String keys so you can continue to use them if you want.

Check out the project on Github.