richjdsmith

richjdsmith

Checking param is present in phoenix controller function

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! :smiley:

Marked As Solved

blatyo

blatyo

Conduit Core Team

You could match like so:

  def show(conn, %{"m" => messenger} = params) do
    # do something with params

    conn
    |> assign(:message, messenger)
    |> render(:show)
  end

or

  def show(conn, %{"m" => messenger, "other" => other}) do
    # do something with other

    conn
    |> assign(:message, messenger)
    |> render(:show)
  end

I believe a function head match error results in a 400, while a match error in the body results in a 500. So, if you want an accurate API response, it’s simpler to do all the matching of required params in the function head. Otherwise, you’ll want to be more conservative in the function body when checking params. Like you mentioned, another valid approach is to use a changeset.

Where Next?

Popular in Questions Top

qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
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

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
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
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement