richjdsmith

richjdsmith

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:

Showing Posts 1 to 2

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.

richjdsmith

richjdsmith OP

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!

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews