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.