Topic: autocomplete problems

Hi,

I've created a form that creates multiple text fields via ajax. I've taken the code from the railscast episode #73 "Complex Forms - Part 2". In addition I extended each of those text fields with an autocomplete function. Since the id of them should be unique I've chosen to use object_id  (in the example the object_id of the task object). When the page is called I have three text fields that use all the same controller function to request the data for the autocomplete. Unfortunately I do not know how the controller can access the value the user has typed into the text field. The only variable I can request is via params[:fieldname] and it returns the id of the text field but not its value.

Could somebody help me with this?

Re: autocomplete problems

This is not a direct answer to your question as it's hard for me to understand exactly what you're doing or not doing without your code.  However, there is a fantastic little script.aculo.us function that is faster than traditional auto-complete provided you don't mind using it's logic (which is pretty good) for determining matches.  Check this out.It's a localized auto-completer but can be populated with the result of one Ajax query rather than a query each time a new character is typed.  Here is an example view and controller.

View

#The start of the form
<%= tag.text_field :tagName, :autocomplete => "off", :size => "25" %>
<div class="auto_complete" id="tag_tagName_list" style="display:none"></div>
#The rest of the form
<script type="text/javascript">
new Ajax.Request('/tags/auto_completion', {
                             method: 'get',
                             onSuccess: function(response) {
                               new Autocompleter.Local('tag_tagName', 'tag_tagName_list', eval(response.responseText), {});
                             }
                           });
</script>

Controller
def auto_completion
  render :json => Tag.list_with_radlex.to_json
end

The list_with_radlex function returns a list with over 32,000 items.  Even on my PC environment it's very fast.

If art interprets our dreams, the computer executes them in the guise of programs.

-SICP (Abelson, Sussman)

Re: autocomplete problems

Hi,

thanks for your answer but I think that I haven't explained my problem enough to have a clear picture. So here some example code. Hope this helps:

Main View

...
<div id="side_1">
<%= render :partial => 'booking_detail', :collection => @booking.booking_details %>
</div>
<%= link_to_function name do | page |
      page.insert_html :bottom, 'side_1', :partial => "booking_detail", :object => BookingDetail.new
    end
%>

Partial "_booking_detail.rhtml"
<% fields_for "booking[booking_details][]", booking_detail do |bd_form| %>
  <div class="booking_detail">
    <% object_id = booking_detail.object_id.abs.to_s%>
    <%= text_field_tag "text_" + object_id  %>&nbsp;
    <%= link_to_function "remove", "$(this).up('.booking_detail').remove()", :class => "remove" %>
    <div class="autocomplete" id="<%= 'auto_complete_' + object_id %>"></div>
    <%= auto_complete_field "text_" + object_id,
      :update => "auto_complete_" + object_id,
      :url => {:action => "autocomplete_account_pretty_name"},
      :with => "'fieldname=' + value",
      :min_chars => 3 %>
  </div>
<% end %>

Controller
...
  def autocomplete_account_pretty_name
    #how can i request the parameter that has been typed in
    param = params[params[:fieldname]]
    @accounts = Account.find(:all, :conditions => "name like '#{param}%'")
    render :partial => "autocomplete_account_pretty_name", :layout => false
  end

...