Pass extra value through text_field_with_auto_complete?


#1

Hi,

I’m wondering if it is possible to pass an extra value (which I will
use to further restrict my query) using text_field_with_auto_complete?

Any suggestions would be appreciated!

-Mike


#2

I have used the following in one of my partials to get this
fuctionality. I
have written a custom auto_complete_for_project_name to handle the
query.
Note the :with option for the auto_complete_field method is text that is
then interperated as javascript.

The View

<label for="customer">Customer</label>
<%= text_field_with_auto_complete :customer, :name %>




<label for="project">Project</label>
<%= text_field :project, :name%>
<div id="auto_complete_for_project_name" 

class=“auto_complete”>
<%= auto_complete_field “project_name”, { :url => {:action =>
‘auto_complete_for_project_name’},
:update =>
“auto_complete_for_project_name”,
:with => “‘customer_name=’
+
$(‘customer_name’).value + ‘&project_name=’ + $(‘project_name’).value” }
%>

And in the controller. I got this from the rails auto_complete_for
definition (the first part of the original is commented out!)

def auto_complete_for_project_name
# define_method(“auto_complete_for_#{object}_#{method}”) do
cust = Customer.find_by_name(params[:customer_name]) ||
Customer.new
find_options = {
:conditions => [ “name LIKE ? AND customer_id = ?”, ‘%’ +
params[:project_name] + ‘%’, cust.id ],
:order => “name ASC”,
:limit => 10 }
@items = Project.find(:all, find_options)
render :inline => “<%= auto_complete_result @items, ‘name’
%>”
end

I have no idea if this is the right Rails way but it works for me so
far.
Although if anyone out there can come up with something better I’m all
ears
:slight_smile:


#3

Daniel ----- wrote:

I have used the following in one of my partials to get this
fuctionality. I
have written a custom auto_complete_for_project_name to handle the
query.
Note the :with option for the auto_complete_field method is text that is
then interperated as javascript.

This looks like it may do exactly what I need - thank you for the quick
reply!