At SO, I have found this solution:
def by_code(conn, %{"code" => code} = params, user) do
prefix = Map.get(params, "prefix", nil) # prefix has a default value
...
end
But is there any better way to do this?
At SO, I have found this solution:
def by_code(conn, %{"code" => code} = params, user) do
prefix = Map.get(params, "prefix", nil) # prefix has a default value
...
end
But is there any better way to do this?
You could merge the params with the defaults:
defaults = %{ "prefix" => nil }
params = Map.merge(defaults, params)
Or you can define Ecto embedded_schema
for the params and supply a default value for each field.