Just getting familiar with Elixir & Phoenix and was hoping for some clarity and direction. First, is it a good habit to have multiple controller functions of the same name and just pattern match against certain params or lack of params? If so, how would I make something like this:
def show(conn, params) do
conn
|> assign(:message, params["m"])
|> render(:show)
end
def show(conn, _params) do
conn
|> assign(:message, "there")
|> render(:show)
end
work? As in, how can I ensure a certain parameter (in this case for simplicity, just the parameter “m”) is present, and if not, skip over it to the next function that matches. Because at the moment, I could pass the parameter “?me=Mike” and it would match with the first function.
I realize that I can do something like:
def show(conn, %{"m" => messenger}) do
conn
|> assign(:message, messenger)
|> render(:show)
end
but am curious if it is possible to pattern match with just a def action(conn, params)
variable instead (so that I can test against multiple params if need be).
I also realize most parameter based logic will be handled by Ecto.Schema, but want to know if this is possible.
Thanks for taking the time to help this newb!