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?
Thanks!
Jen
Code:
=======main_form.rhtml:
------------
<h1>Test</h1>
<%= start_form_tag :controller => ''contact'', :action
=> ''res'' %>
<div id="contact_div">
<p>No contact currently selected</p>
<input type=''hidden''
name=''contact_id'' value="bad_value">
</div>
<%= submit_tag %>
<%= end_form_tag %>
<div id="search_div">
<%= render :partial => "contact/search_form" %>
</div>
------------
=======contact_controller.rb:
------------
class ContactController < ApplicationController
def search
search_term = params[:search_term] || ''''
@results = Contact.find_all ["first_name like ?",
"%#{search_term}%"]
render :partial => "search_form"
end
def set_contact
id = params[:id]
@contact = Contact.find id
render :layout => false
end
end
------------
=======set_contact.rhtml:
------------
<div id="contact_div">
<p><%= "#{@contact.first_name} #{@contact.last_name}"
%></p>
<input type="hidden" name="contact_id"
value="<%= @contact.id %>">
</div>
------------
=======_search_form.rhtml
------------
<head>
<%= javascript_include_tag "prototype" %>
</head>
<body>
<%= form_remote_tag :update => "search_div",
:url => { :controller => "contact", :action
=>
"search" } %>
<label for="search">Search: </label>
<%= text_field_tag :search_term %>
<%= submit_tag "Find" %>
<%= end_form_tag %>
<% if !@results.nil?
@results.each do | contact | %>
<%= link_to_remote "#{contact.first_name} #{contact.last_name}",
:update => "contact_div",
:url => { :action =>
''set_contact'', :id =>
contact.id } %>
<br />
<% end %>
<% end %>
</body>
------------