Text_field_with_auto_complete and record ID


#1

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


#2

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 %>

<%= link_to ‘New supplier’, {:icon => ‘user_add’, :controller =>
‘/admin/parties’, :action => :new}, {:target => ‘_new’} %>

app/views/products/auto_complete_for_supplier_name.rhtml:

    <%= render :partial => 'supplier', :collection => @suppliers %>

app/views/products/_supplier.rhtml:

  • <%= h(supplier.display_name) %>
  • This was built on Rails 0.12 originally, and hasn’t changed since. It
    should probably be revisited to use more helpers.

    Bye !


    #3

    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?


    #4

    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/actionpack/lib/action_view/helpers/java_script_macros_helper.rb#L146

    Bye !