Not to pass the buck, but you would do well to read @LostKobrakai’s article One-to-Many LiveView Forms. It even uses an embedded schema for the (interactive) example!
Your template should end up looking something like this:
Of course I’m leaving out some of the checks—I just threw in the required check as an example—but the main point is is that there should be no need to add and reference indices nor should you have to manually check for the “selected” option. All that is taken care of for you using an embedded schema with <.inputs_for />.
Some other small notes:
You’ll have to switch around how you’re building your year_options and kind_options. The options attribute for <.input type="select" /> takes them in the form of [{"label", :value}]
It’s better to pass structs to verified routes instead of the id explicitly:
It probably doesn’t matter for your project but if you ever decide to use another field (like slug) for URL params, all you’d have to do is add @derive {Phoenix.Param, key: :slug} to your schema and everything else will be taken care of.
PS, there is a new way of making forms like this that I still haven’t tried since I haven’t felt the pain of this way yet.