Forum: Ruby on Rails text_field_with_auto_complete and record ID

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.
liststuff (Guest)
on 2005-12-05 14:35
(Received via mailing list)
I understand it's not possible to retreive the ID of a record selected
using
text_field_with_auto_complete, but I'm struggling to create my own
version
of the helper with that functionaility.

has anyone managed to do this? if someone has a code sample, I'd be very
grateful
francois.beausoleil (Guest)
on 2005-12-05 20:24
(Received via mailing list)
Hello Alan !

2005/12/5, Alan B. <removed_email_address@domain.invalid>:
> I understand it's not possible to retreive the ID of a record selected using
> text_field_with_auto_complete, but I'm struggling to create my own version
> of the helper with that functionaility.
>
> has anyone managed to do this? if someone has a code sample, I'd be very
> grateful

What I did is in the action that does the retrieval, I set the LI's ID
attribute to the record ID.  Then, I have an afterUpdate callback that
copies the ID in a hidden field.

Something like this:

app/views/products/_form.rhtml:
<%= hidden_field :product, :supplier_id %>
<%= text_field :supplier, :name, :size => 40, :value =>
@product.supplier.nil? ? nil : @product.supplier.display_name %>
<br/><%= link_to 'New supplier', {:icon => 'user_add', :controller =>
'/admin/parties', :action => :new}, {:target => '_new'} %>
<div class="auto_complete" id="supplier_name_auto_complete"/>
<script type="text/javascript">
  new Ajax.Autocompleter('supplier_name', 'supplier_name_auto_complete',
        '/admin/product_catalog/products/auto_complete_for_supplier_name',
        {afterUpdateElement: function(element, selectedElement) {
            $('product_supplier_id').value =
selectedElement.id.substring(1);}});</script>

app/views/products/auto_complete_for_supplier_name.rhtml:
<ul>
<%= render :partial => 'supplier', :collection => @suppliers %>
</ul>

app/views/products/_supplier.rhtml:
<li id="s<%= supplier.id %>"><%= h(supplier.display_name) %></li>

This was built on Rails 0.12 originally, and hasn't changed since.  It
should probably be revisited to use more helpers.

Bye !
oom tuinstoel (Guest)
on 2006-05-22 15:47
Has anyone ever rewritten this, using "text_field_with_auto_complete"?

I am struggling with it, but can't get it to work. I can retrieve a
String, but not the ID. Help, anyone?
François B. (Guest)
on 2006-05-22 16:25
(Received via mailing list)
Hi !

2006/5/22, oom tuinstoel <removed_email_address@domain.invalid>:
> Has anyone ever rewritten this, using "text_field_with_auto_complete"?
>
> I am struggling with it, but can't get it to work. I can retrieve a
> String, but not the ID. Help, anyone?

If I remember correctly, auto_complete_field now has support for
afterUpdateElement.  Look in the code:
http://dev.rubyonrails.org/browser/trunk/actionpac...

Bye !
This topic is locked and can not be replied to.