Forum: Ruby on Rails Ajax -- select from search results

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
jennyw (Guest)
on 2005-11-14 08:18
(Received via mailing list)
I have a form that includes a field for a contact. There's a huge number
of contacts, so instead of having it be a drop down, I want to be able
to let people search. This seems like it'd be perfect for Ajax.

In my Contacts controller, I have a method called search. This, combined
with a view, returns a list of search results. I'd like it so that if
someone clicks on a contact, it would set a hidden field to the value of
the id of the contact. I actually have this working, but I'm wondering
if there's a better way, since this seems pretty cumbersome.

What I've done is this (code follows this message). I have a form
(main_form.rhtml) that includes a hidden field called "contact_id". The
hidden field is inside a div called "search_div". This view includes a
partial template for the Ajax form, contained in _search_form.rhtml,
inside of which is a form created by form_remote_tag. When you type in a
term and press the search button, it goes to the search action of the
Contacts controller, which returns some results. _search_form renders
these results as a list of links. The links are generated using
link_to_remote such that when clicked, they replace "search_div" (which
includes both the full name of the contact followed by the hidden form
field w/ the value set to the id of the contact).

Like I said, this works, but it really seems like there should be an
easier way, and also a way where the pieces aren't so tightly coupled
(e.g., for this to work, the original page has to have a div called
"search_div"). Any suggestions?



This topic is locked and can not be replied to.