wdtnh
Desperate to get this parent lookup working in simple form .input type=select
I have a phoenix app with a lot of related tables. In particular, I have a regions (think territory) as a parent table that has a related child table consisting of 3-digit zip codes that are in a table region_zips.
I need a select form element in the region_zip form (created by phx.gen.html) in order to make sure I’m picking the correct region for a particular 3 digit zip code. I cannot get this to work no matter how many ways I’ve tried. I know the lookup in def new(conn, _params) works. It’s just not working in the form. I would greatly appreciate any assistance. Thanks in advance. Bill
def new(conn, _params) do
regions = Regions.lookup_regions() # Valid results are [%{id: 1, name: "Test" },...]
changeset = RegionZips.change_region_zip(%RegionZip{})
render(conn, :new, changeset: changeset, regions: regions)
end
The related form is
<.simple_form :let={f} for={@changeset} action={@action}>
<.error :if={@changeset.action}>
Oops, something went wrong! Please check the errors below.
</.error>
<.input field={f[:region_id]} type="select" options={@regions} label="Region" />
<.input field={f[:zip_prefix]} type="text" label="Zip prefix" />
<:actions>
<.button>Save Region zip</.button>
</:actions>
</.simple_form>
First Post!
wdtnh
Appreciate the reply but that wasn’t it. I’ve formatted the regions lookup in a number of ways as an array of maps, tuples, keywords, i, etc. I still get key error message that lookup of regions passed as an arg to the render function executes.
KeyError at GET /region_zips/new
key :regions not found in: %{
action: "/region_zips",
__given__: %{
action: "/region_zips",
__changed__: nil,
changeset: #Ecto.Changeset<
action: nil,
changes: %{},
errors: [
region_id: {"can't be blank", [validation: :required]},
zip_prefix: {"can't be blank", [validation: :required]}
],
data: #Crm.RegionZips.RegionZip<>,
valid?: false,
...
>
},
Last Post!
wdtnh
I’ve been traveling & haven’t had time to dig into this but I believe you hit the nail on the head of what my problem was. I absolutely love Elixir but sometimes I get lost how things get passed along. In the related region_zips VIEW field, I will pass regions as an attribute which I failed to do. I see where I was previously passing in as an action. I’ve already corrected for the proper formatting of the options in terms of key and value pairs. Thanks for the assist it is greatly appreciated.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









