Forum: Ruby on Rails auto_complete_field help

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.
Andrew F. (Guest)
on 2006-05-23 21:26
(Received via mailing list)
Not sure what I am doing wrong. Here is what i am trying to do (code
to follow).

I have a form to create a new book. This book can have many authors
and I wanted to be able to add the authors based on if they appear in
the authors table. I originally was going about it a long and arduous
way where I had several forms. After thinking about it I decided to
dynamically add new fields to the form for the authors. To do this I
created an action that renders a partial which is just a
text_field_tag to the form. But I want this text field to use
autocomplete. Here is the code (hope it explains better what I am
trying to do)

#View

# This is the first link when the form is first generated there are
no text fields for authors (it is not required to add one)

<%= link_to_remote("Add", :url => {:controller => "book", :action =>
"ajax_add_credit_to_form", :credit_number => 0}) %>

# Controller

#This action figures out if it is the first time I am adding an
author or subsequent times after that and than either adds the UL or
just inserts
# a new li

def ajax_add_credit_to_form
     @credit_number = (params[:credit_number]).to_i
     render :update do |page|
       if @credit_number == 0
         page.replace_html 'credits_list', :partial =>
'new_credit_field'
       else
         page.insert_html :bottom, 'the_credit_list', :partial =>
'new_credit_field'
       end
     end
  end

# Partial

# This is where I am running into issues

<% if @credit_number == 0 %>
	<ul id="the_credit_list">
		<li id="credit_<%= @credit_number %>">
			<%= text_field_tag("#{@credit_number}creator", nil, {:size =>
'15'}) %>
			<%= auto_complete_field("#{@credit_number}creator", :url =>
{:action => "auto_complete_for_author_name"}, :with => "#
{@credit_number}creator") %>
			<%= link_to_remote("Add", :url => {:controller => "book", :action
=> "ajax_add_credit_to_form", :credit_number => (@credit_number +
1)}) %>

		</li>
	</ul>
<% else %>
	<li id="credit_<%= @credit_number %>">
			<%= text_field_tag("#{@credit_number}creator", nil, {:size =>
'15'}) %>
			<%= auto_complete_field("#{@credit_number}creator", :url =>
{:action => "auto_complete_for_author_name"}, :with => "#
{@credit_number}creator") %>
			<%= link_to_remote("Add", :url => {:controller => "book", :action
=> "ajax_add_credit_to_form", :credit_number => (@credit_number +
1)}) %>
	</li>
<% end %>

In my controller I have the following set at the top:

auto_complete_for :author, :name

But when I load the page in the browser and I click on add for the
first time I get the following error in firebug:

missing variable nameprototype.js?1144376... (line 181)
var 0creator_auto_completer = new Ajax.Autocompleter('0creator',
'0creator_auto_complete', '/issue/auto_complete_for_author_name',
{callback:function(element, value) { return 0creator }})

Any thoughts? I can't use the text_field_with_autocomplete because I
could have multiple fields with the same name which wont work. I
think I am on the right track but can not figure out what I am doing
wrong. I am pretty sure it is the auto_complete_field tag and I have
tried it a couple of different ways, one way was completely removing
teh :with option but I received the same error.

Thanks for the help

Andrew
Andrew F. (Guest)
on 2006-05-23 22:16
(Received via mailing list)
Well I figured out my issue so figured I would post it. Rather dumb
of me. The error was due to the field starting with a 0 (zero). It
did not like the variable beginning with a number. Also found that
the I needed to add the div tag in for the return which I did not
think I would need as using text_field_with_auto_complete does it for
you. And lastly I needed to create a custom search action in my
controller in order to get the right search. You can't use the
auto_complete_for action when working with dynamic fields the way
that I was.

Sorry for the earlier double post. Mail said that I still had a draft
and so I sent again.

Andrew
This topic is locked and can not be replied to.