Forum: Ruby on Rails pass extra value through text_field_with_auto_complete?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mike O. (Guest)
on 2006-06-02 18:27
(Received via mailing list)
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
Daniel -. (Guest)
on 2006-06-02 18:46
(Received via mailing list)
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"></div>
    <%= 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
:-)
Mike O. (Guest)
on 2006-06-02 19:53
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!
This topic is locked and can not be replied to.