A
A
Alexander Korolev2016-01-04 17:46:41
Ruby on Rails
Alexander Korolev, 2016-01-04 17:46:41

Rails: form with the ability to select an existing one or add a new one, what's the best way to do it?

Good afternoon.
There is a need to have a form on which you can either select data from the existing one (now I'm trying to implement auto-completion) or enter new data (in total there are 4 input fields).
I made an auto-completion, the fields will be filled if there is a match, but when saving, an error occurs. the data does not have an id, because a form for a new entry... Yes, I understand that you can add a node with data by id, but somehow it's not rude to write this: agency_order[passengers_attributes][0][client_attributes][id] in the selector. Maybe there is some practice in this regard: make a select, and next to the "add" button (although in this case the addition will be more than the selection from the database).
Thanks for any thoughts.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
Chronic 86, 2016-01-04
@chronic86

Model.find_or_create_by doesn't work?

V
vsuhachev, 2016-01-05
@vsuhachev

You will have to do everything in javascript. If you need an id, then pass it along with the autocomplete text and then submit the form with the desired id.

E
Evgeniy Trebin, 2016-01-05
@evgeniy_trebin

Maybe I misunderstood the problem, but why save the attributes for the new entry separately from the entry itself, instead of saving the new entry along with the nested attributes?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question