How to set belongs_to on in a form

I have a create action for Plan that sets the Product (Plan belongs_to Product)
create :create do
primary? true
argument :product, :map, allow_nil?: false
change manage_relationship(:product, :product, type: :append)
end
create :create do
primary? true
argument :product, :map, allow_nil?: false
change manage_relationship(:product, :product, type: :append)
end
I have a form that I create like this...
create_plan_form =
AshPhoenix.Form.for_create(Plan, :create, api: Environments)
|> to_form()
|> AshPhoenix.Form.add_form([:product])
create_plan_form =
AshPhoenix.Form.for_create(Plan, :create, api: Environments)
|> to_form()
|> AshPhoenix.Form.add_form([:product])
and then a select input
<.inputs_for :let={product_form} field={@create_plan_form[:product]}>
<.input
type="select"
field={{product_form, :id}}
options={@product_options}
label="Product"
/>
</.inputs_for>
<.inputs_for :let={product_form} field={@create_plan_form[:product]}>
<.input
type="select"
field={{product_form, :id}}
options={@product_options}
label="Product"
/>
</.inputs_for>
This raises an error:
product at path [] must be configured in the form to be used with `inputs_for`.
product at path [] must be configured in the form to be used with `inputs_for`.
I tried forms: [auto?: true] and then I don't get an error but I also don't get any field.
2 Replies
michaelst
michaelst2y ago
Oh I had this same issue, I ended up using the id field and not inputs for, so try changing your action to product_id and use it as a field instead
Robert Graff
Robert GraffOP2y ago
Thanks @michaelst

Did you find this page helpful?