Can text_field_with_auto_complete return ID?

Hello

Can I make text_field_with_auto_complete show the name atribute and then
return the ID of the selected object when the page is posted-back?

Thanks!

Okada.

I am having the same issue…

Can anyone help?

carlos.okada wrote:

Hello

Can I make text_field_with_auto_complete show the name atribute and then
return the ID of the selected object when the page is posted-back?

Thanks!

Okada.

A textfield just returns it’s contents. It’s just how HTML works. What
you can do is match it up server side.

object = SomeModel.find(:first,
:conditions => [‘title = ?’,
params[:data_from_textbox]])

Shai S. wrote:

I am having the same issue…

Can anyone help?

The trick that I use is to return the id as a hidden field. Here is an
example of _full_names.rhtml I used as part of the Contacts demo I am
working on.

    <% for contact in @contacts do -%>
  • <%= contact.id %>
    <%=h "#{contact.first_name} #{contact.last_name}" %>
    <%=h "#{contact.address}" %>
    <%=h "#{contact.city}, #{contact.state} #{contact.zip_code}" %>
  • <% end -%>

I then take the resulting value returned when the user clicks the
appropriate item and convert it to an integer which I can use to find
the correct Contact entry. Using this method I can then update multiple
fields.

Alex W. wrote:

carlos.okada wrote:

Hello

Can I make text_field_with_auto_complete show the name atribute and then
return the ID of the selected object when the page is posted-back?

Thanks!

Okada.

A textfield just returns it’s contents. It’s just how HTML works. What
you can do is match it up server side.

object = SomeModel.find(:first,
:conditions => [‘title = ?’,
params[:data_from_textbox]])

This only works if the text contents returned are unique enough. If your
data is more complex or if you have multiple similar entries, this
doesn’t work well.

Dale M. wrote:

The trick that I use is to return the id as a hidden field. Here is an
example of _full_names.rhtml I used as part of the Contacts demo I am
working on.

    <% for contact in @contacts do -%>
  • <%= contact.id %>
    <%=h "#{contact.first_name} #{contact.last_name}" %>
    <%=h "#{contact.address}" %>
    <%=h "#{contact.city}, #{contact.state} #{contact.zip_code}" %>
  • <% end -%>

I then take the resulting value returned when the user clicks the
appropriate item and convert it to an integer which I can use to find
the correct Contact entry. Using this method I can then update multiple
fields.

I understand the concept and I tried out some code, but it does not seem
to work as in the example… Would you be able to post a little of your
controller and view code so I could compare and possibly see what I did
wrong?

To help better understand, Ill post mine as well…

I want to search for a product title and when I click “see” I want to go
to that products page…

In my view I have:

<%= start_form_tag :action => ‘view’, :id => @search_item %>

<%= text_field_with_auto_complete :product, :title, :size => 40 ,
:skip_style => true %>

<%= end_form_tag %>

And then, in the Controller:

def auto_complete_for_product_title
auto_complete_responder_for_products params[:title]
end

private

def auto_complete_responder_for_products(value)
@search_results = Product.find(:all,
:conditions => [ ‘LOWER(title) LIKE ?’,
‘%’ + value.downcase + ‘%’ ],
:order => ‘title ASC’,
:limit => 8)
render :partial => ‘products’
end

And in my partial:

    <% for search_item in @search_results do -%>
  • <%= search_item.id %>
    <%= image_tag url_for_file_column "search_item", "art", "50" -%>
    <%= search_item.title %>
  • <% end -%>

Could anyone offer some insight as to what may be wrong? I will say that
when looking at the scriptaculous example I do get a bit confused with
the :message params that are passed… what are they needed for?

Thanks

I forked DHH’s auto_complete plugin to add the record id to the id tag
of the

  • element. There is an example in the README of how to use the
    id using :after_update_element. Check it out on GitHub at
    GitHub - jmaziarz/auto_complete: Auto Completion plugin.

  • Shai S. wrote:

    Dale M. wrote:

    The trick that I use is to return the id as a hidden field. Here is an
    example of _full_names.rhtml I used as part of the Contacts demo I am
    working on.

      <% for contact in @contacts do -%>
    • <%= contact.id %>
      <%=h "#{contact.first_name} #{contact.last_name}" %>
      <%=h "#{contact.address}" %>
      <%=h "#{contact.city}, #{contact.state} #{contact.zip_code}" %>
    • <% end -%>

    I then take the resulting value returned when the user clicks the
    appropriate item and convert it to an integer which I can use to find
    the correct Contact entry. Using this method I can then update multiple
    fields.

    I understand the concept and I tried out some code, but it does not seem
    to work as in the example… Would you be able to post a little of your
    controller and view code so I could compare and possibly see what I did
    wrong?

    I posted a description of what I am doing on my blog. See if this helps,
    if not I will create a demo Rails app for you to look at.

    http://www.dalemartenson.com/blog/?p=24

    Here is a the parts that I use to get the id of an object and update
    multiple fiields.

    index.rthml-------------------

    <%= javascript_include_tag :defaults %>

    Look-up Contact By Full Name: <%= render :partial => 'get_full_name' %>


    <%= render :partial => 'get_name_info' %> <%= render :partial => 'get_address_info' %> <%= render :partial => 'get_phone_info' %> <%= render :partial => 'get_internet_info' %>

    EOF---------------------------

    I like using render :partial for all the pieces of the displayed page.
    The autocomplete field is in the _get_full_name.rthml partial.

    _get_full_name.rhtml----------

    <%= text_field_with_auto_complete :contact, :full_name, {}, :after_update_element => "function(element,value) " + "{ " + remote_function(:update=>'full_name',:url=>{:action=>:get_full_name},:with=>"'id='+element.value") + ";" + remote_function(:update=>'name_info',:url=>{:action=>:get_name_info},:with=>"'id='+element.value") + ";" + remote_function(:update=>'address_info',:url=>{:action=>:get_address_info},:with=>"'id='+element.value") + ";" + remote_function(:update=>'phone_info',:url=>{:action=>:get_phone_info},:with=>"'id='+element.value") + ";" + remote_function(:update=>'internet_info',:url=>{:action=>:get_internet_info},:with=>"'id='+element.value") + ";" + "}" %>

    EOF---------------------------

    This uses the after_update_element to force the updating of other
    corresponding pieces of the page being displayed. The id is past from
    the element.value which is really a string based on list item contents
    that were shown to the user. This generated Javascript function controls
    getting the data for the updates and helps to perform the multiple
    partial updates.

    Note: I reference full_name as if it were a field in my contacts table,
    but it isn’t. I have added a member function to the Contacts model to
    combine the first_name and last_name fields into a full_name.

    def full_name
    “#{self.first_name} #{self.last_name}”
    end

    Since I want to perform full name searches using a single field, I
    require a custom handler be added to my controller.

    def auto_complete_for_contact_full_name
    @contacts = Contact.find(:all,
    :conditions => [ ‘LOWER(CONCAT(first_name," ",last_name)) LIKE ?’,
    ‘%’ + params[:contact][:full_name].downcase + ‘%’ ],
    :order => ‘first_name ASC’,
    :limit => 8)
    render :partial => ‘full_names’
    end

    The list item content is controlled by the _full_names.rhtml partial in
    this example.

    _full_names.rhtml--------------

      <% for contact in @contacts do -%>
    • <%= contact.id %>
      <%=h "#{contact.first_name} #{contact.last_name}" %>
      <%=h "#{contact.address}" %>
      <%=h "#{contact.city}, #{contact.state} #{contact.zip_code}" %>
    • <% end -%>

    EOF---------------------------

    Rather than just display a simple list of names. I display a bold full
    name and a complete address in a small font to help uniquely identify
    the individual. But the important part is what is not seen by the user.
    The first part of my list item is a non-displayed text string of the
    object id. This uniquely identifies the item without having to re-query
    the database which can be problematic depending on the data uniqueness
    of the data and if multiple entries may exist.

    The string returned when the user selects an entry looks something like
    “\n 123\n Dale M.\n 1234 Blah Street\n Somewhere, Someplace
    12345”. As you will see, I can take thing string and convert it to an
    integer to ‘steal’ the first id portion while ignoring the rest. From
    the generated Javascript above, this is the result in element.value that
    I pass as a parameter back to my controller methods.

    All the controller methods are very similar.

    def get_address_info
    @contact = Contact.find(params[:id].to_i)
    render :partial => ‘get_address_info’
    end

    As you can see, it takes the id parameter converts it to an integer and
    finds the correct object based on the id. Then the corresponding partial
    is rendered to complete the updating of that portion of the display.

    _get_address_info.rhtml-------

    Address Information Address: <%= text_field :contact, :address %>
    City: <%= text_field :contact, :city %> State: <%= text_field :contact, :state %> Zip: <%= text_field :contact, :zip_code %>

    EOF---------------------------

    There may be a more elegant way to do the update, but I have yet to
    determine one. If anyone has any suggestions for improving this please
    let me know.