How to specify default parameters for controller actions

phoenix
router
controller
Tags: #<Tag:0x00007f039bc6e878> #<Tag:0x00007f039bc6e710> #<Tag:0x00007f039bc6e5a8>

#1

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?


#2

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.