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
Hello Alan ! 2005/12/5, Alan Bullock <liststuff@gmail.com>:> 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 > gratefulWhat 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 ! -- François Beausoleil http://blog.teksol.info/ _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
oom tuinstoel
2006-May-22 11:47 UTC
[Rails] Re: text_field_with_auto_complete and record ID
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? -- Posted via http://www.ruby-forum.com/.
Francois Beausoleil
2006-May-22 12:23 UTC
[Rails] Re: text_field_with_auto_complete and record ID
Hi ! 2006/5/22, oom tuinstoel <oomtuinstoelNO@spamhotmail.com>:> 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 ! -- Fran?ois Beausoleil http://blog.teksol.info/