hendri-tobing

hendri-tobing

I have database tables: Companies, Devices, and Signs. The device belongs_to a Company and the Company has_many Signs.

I want to show on the new device form:

  • a select box of the existing Companies, and
  • a select box of the Signs that is populated based on the company selection.

The select box of the signs is dynamically changed if the selected company changed.

How to achieve this?

So far this is my device_controller.ex

defmodule PortalWeb.DeviceController do
  use PortalWeb, :controller

  alias Portal.Fleet
  alias Portal.Fleet.Device


  def create(conn, %{"device" => device_params}) do
    with {:ok, %Device{} = device} <- Fleet.create_device_customer(device_params) do
      conn
      |> put_flash(:info, "The device is created successfuly")
      |> redirect(to: Routes.page_path(conn, :index))      
    end
  end

  def new(conn, _params) do
    changeset = Fleet.change_device(%Device{})
    customers = Fleet.list_customers()
    |> Enum.map(&{"#{&1.name}", &1.id})
    render(conn, "new.html", changeset: changeset, customers: customers)
  end
end

Thank you.

Showing Posts 1 to 6

lucaong

lucaong

You will have to use either JavaScript or LiveView for this. Any preference between these two options?

hendri-tobing

hendri-tobing OP

@lucaong do you have any advise how to achieve it by using LiveView?

lucaong

lucaong

Honestly I am not a LiveView expert (been meaning to give a detailed look into it soon), so hopefully someone else can pitch in :slight_smile:

sfusato

sfusato

Basically you would have a phx-change event on the form which will fire on every input change. Then in the handle_event callback you will pattern match on your Company select value and see if it changed from the one you have in your socket assigns. If it did, fetch the new Signs and re-assign. This will re-render your liveview automatically.

While there isn’t an example exactly like your use-case in it, phoenix_live_view_example is a good way to peek at how things work.

Later edit: found this (the “Bike comparison” example)

hendri-tobing

hendri-tobing OP

Thank you for the suggestion and example. I managed to populate the select field by using LiveView.

As suggested, I use phx-change that will pattern match the Company select value, then fetch the new Sign based on the value. Also assign the selected value to the Company select field.

This is part of my LiveView file.

  def render(assigns), do: LiveDeviceView.render("form.html", assigns)

  def mount(session, socket) do
    companies = Portal.Fleet.list_companies()
    selected_company = nil
    
    {:ok,
     assign(socket,
       changeset: Fleet.change_device(%Device{}),
       companies: companies,
       selected_company: selected_company,
       signs: [],
     )}
  end

  def handle_event("populate_sign", %{"device" => params}, socket) do
    signs = Fleet.get_company(String.to_integer(params["company_id"]))
    companies = Portal.Fleet.list_companies()
    changeset = Fleet.change_device(%Device{})
    selected_company = String.to_integer(params["company_id"])

    {:noreply, assign(socket, signs: signs, companies: companies, selected_company: selected_company, changeset: changeset)}
  end

form.html.leex

<%= f = form_for @changeset, "#", [phx_change: :populate_sign, phx_submit: :save] %>

  <%= label f, :company_id %>
  <%= select f, :company_id, Enum.map(@companies, &{&1.name, &1.id}), prompt: "Select a company", selected: @selected_company %>
  <%= error_tag f, :companies %>

  <%= if @signs == [] do %>
    <%= if @selected_company != nil do %> 
      <p>This company has no Sign</p> 
      <%= link "Add a new sign for this company", to: Routes.company_path(@socket, :new) %>
    <% end %>
  <% else %>
    <%= label f, :sign_id %>
    <%= select f, :sign_id, Enum.map(@signs, &{&1.name, &1.id}), prompt: "Select a sign" %>
    <%= error_tag f, :signs %>
  <% end %>

    <%= submit "Save", phx_disable_with: "Saving..." %>
  </div>

</form>

Thank you again for all responses.

vecuenca

vecuenca

Just chiming in several years later to say this helped me solve a similar frustrating issue I had. Thanks!

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews