Let's say I have a dropdown selection list of existing teachers. The
user can (1) select any one teacher, or (2) create a new one.
How would you tackle something like that?
I'm thinking what I could do is check if there's any data inputted into
the "new teacher" field -- and if there is, create a new teacher. But,
the issue with that is the user might also have an existing teacher
highlighted, so that creates confusion -- (not to mention bad
usability!)
You could use auto_complete_field to have a simple text field. They type in the name, it pulls up a list. If they don't like one of those options, they finish typing and (assuming you only need the teacher's name) you create a new teacher record when they submit the form.
If you need more information, I'd have a "Other/New" option in the select box, and have it overlay a div asking for that info like the other poster suggested.
Bob Sanders wrote:
Philip and Mohit,
Thank you for the replies! I'm going to try all of your various ideas and see which works best. The AJAX one sounds really cool, and I think would be very use-friendly if done right.
Of course, relying on my skills isn't the best, so we'll see what happens 
Thanks again!
Bob,
If you do get the AJAX one done, do consider sharing 
Cheers,
Mohit.
8/26/2007 | 5:03 PM.
I do this in quite a few different places in my app.. In one
instance, an Event belongs_to a venue, so I give the user the option
to select any venue from a list, or they can create a new one by
clicking on the "Create new venue..." option in the dropdown (I use a
javascript onclick handler to detect when they select the appropriate
option). I then display a hidden div which contains a form for
creating a new venue.
An event can also be associated with an artist, so I use an ajax
autocomplete field to display a list of artists. I then use something
like @event.artist =
Artist.find_or_initialize_by_artist_name(params[:artist_name]) which
will either retrieve the Artist with the given name, or it'll create a
new instance, which will then be saved when I call @event.save.
So the Ajax code isn't necessarily very complicated, it's just a
standard autocomplete field and uses find_or_initialize_by_*.
Adam