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.

Last Post!

richjdsmith

richjdsmith

This is perfect. Thanks very much for explaining that. I didn’t even consider the consequences of API error responses so thanks for giving me something to consider!

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement