# Text\_field\_with\_auto\_complete problem

**URL:** <https://www.ruby-forum.com/t/text-field-with-auto-complete-problem/124983>\
**Category:** Rails\
**Created:** [January 4, 2008, 9:54am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-problem/124983 "2008-01-04T09:54:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![damien](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de053/32.png) [@damien](https://www.ruby-forum.com/u/damien)\
**Post date:** [January 4, 2008, 9:54am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-problem/124983/1 "2008-01-04T09:54:18Z")

</div>

I can’t get the autocomplete feature of [script.aculo.us](http://script.aculo.us) to work. I  
have tried every guide I could find and several books to no avail.

As you can probably tell from the code, I’m new to this and just used  
the scaffolding feature to setup a framework I was planning on  
altering. Things I’ve tried so far to no avail are removing the  
auto\_complete function from the controller and removing the  
javascript\_include\_tag from the view. Regardless of those changes,  
when I type the first letters of a valid name into the  
text\_field\_with\_auto\_complete it does not give me any suggestions. Any  
help would be most appreciated!

Below are excerpts from my controller and my view file:

##########################  
#ErrorsController  
##########################  
class ErrorsController \< ApplicationController  
auto\_complete\_for :error, :name

# GET /errors

# GET /errors.xml

def index  
@errors = Error.find(:all)  
respond\_to do |format|  
format.html # index.html.erb  
format.xml { render :xml =\> @errors }  
end  
end

# GET /errors/1

# GET /errors/1.xml

def show  
@error = Error.find(params[:id])

```
respond_to do |format|
  format.html # show.html.erb
  format.xml { render :xml => @error }
end

```

end

# GET /errors/new

# GET /errors/new.xml

def new  
@error = Error.new

```
respond_to do |format|
  format.html # new.html.erb
  format.xml { render :xml => @error }
end

```

end

# GET /errors/1/edit

def edit  
@error = Error.find(params[:id])  
end

# POST /errors

# POST /errors.xml

def create  
@error = Error.new(params[:error])

```
respond_to do |format|
  if @error.save
    flash[:notice] = 'Error was successfully created.'
    format.html { redirect_to(@error) }
    format.xml { render :xml => @error, :status

```

=\> :created, :location =\> @error }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @error.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end

# PUT /errors/1

# PUT /errors/1.xml

def update  
@error = Error.find(params[:id])

```
respond_to do |format|
  if @error.update_attributes(params[:error])
    flash[:notice] = 'Error was successfully updated.'
    format.html { redirect_to(@error) }
    format.xml { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml { render :xml => @error.errors, :status

```

=\> :unprocessable\_entity }  
end  
end  
end

# DELETE /errors/1

# DELETE /errors/1.xml

def destroy  
@error = Error.find(params[:id])  
@error.destroy

```
respond_to do |format|
  format.html { redirect_to(errors_url) }
  format.xml { head :ok }
end

```

end

end  
#######################  
#index.html.erb  
#######################

# Listing errors
\<%= javascript\_include\_tag :defaults %\> \<%= text\_field\_with\_auto\_complete :error, :name %\> \<% for error in @errors %\> \<% end %\>

| Name | Description | Solution |
| --- | --- | --- |
| \<%=h error.name %\> | \<%=h error.description %\> | \<%=h error.solution %\> | \<%= link\_to 'Show', error %\> | \<%= link\_to 'Edit', edit\_error\_path(error) %\> | \<%= link\_to 'Destroy', error, :confirm =\> 'Are you sure?', :method =\> :delete %\> |

  

\<%= link\_to ‘New’, new\_error\_path %\>  
#######################

---

<div class="post-metadata">

**Author:** ![damien](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de053/32.png) [@damien](https://www.ruby-forum.com/u/damien)\
**Post date:** [January 4, 2008, 10:34am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-problem/124983/2 "2008-01-04T10:34:06Z")

</div>

I should probably also mention that I did install the plug-in as per  
the direction of some guide I found on the internet.

---

<div class="post-metadata">

**Author:** ![damien](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de053/32.png) [@damien](https://www.ruby-forum.com/u/damien)\
**Post date:** [January 4, 2008, 10:36am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-problem/124983/3 "2008-01-04T10:36:27Z")

</div>

You could try what sandy posted here:

[http://railsforum.com/viewtopic.php?pid=49557#p49557](http://railsforum.com/viewtopic.php?pid=49557#p49557)

---

<div class="post-metadata">

**Author:** ![damien](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de053/32.png) [@damien](https://www.ruby-forum.com/u/damien)\
**Post date:** [January 5, 2008, 5:04am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-problem/124983/4 "2008-01-05T05:04:04Z")

</div>

Is there a better venue than this to get Ruby help?

---

<div class="post-metadata">

**Author:** ![damien](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de053/32.png) [@damien](https://www.ruby-forum.com/u/damien)\
**Post date:** [January 7, 2008, 11:02am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-problem/124983/5 "2008-01-07T11:02:41Z")

</div>

Damien,

debugging these types of things can be tricky, but knowing what to  
look at can help.

auto complete breaks into a number if steps.

1. you enter the characters into the field
2. javascript functions on the page make an ajax request to the  
controller
3. the controller method accesses the model to do the search, formats  
the information and renders it back to the autocomplete list div
4. steps 2 and 3 repeat until you press enter.
5. Pressing enter triggers javascript to either update the web page  
locally, and/or make an ajax call to another controller method.
6. The controller method can then update whatever other part of the  
page it needs to.

I am sure you have already know the above, but spelling it out helps  
to debug the various stages.

You will get valuable help from Firefox-Firebug extension and the  
development log. Inspecting these will show you where the process is  
failing. I normally do the following, not necessarily in the same  
order.

1. Look at the log to see if a call to the controller auto\_complete  
\_for method has been made. If not, then the javascript stuff on the  
page is not working. If it has, then has rails thrown an error.
2. Look at the request and response in Firebug to see if the update  
information is getting back to the page.
3. Use the firebug inspector to look at the javascript that rails has  
generated and see that the url etc makes sense. You dont need to be a  
js guru to see roughly what is going on.
4. If you cannot make sense of 3, or need more detail, look at the  
scriptacutulous info on autocompleter.
5. The bit I find hardest to get at is the controller method that is  
automatically created, but it is possible to write your own controller  
method to handle the auto complete, and if you use a render :update  
here, you can do things like use an Alert box to show info in the  
browser. I often cut and paste the source of the auto complete for  
method so that I have something concrete in my controller to look at  
and adapt.

finally, looking at the code you have given, the auto complete stuff  
is straight forward. I notice you have included the javascript  
defaults in the body rather than the header. It would be more normal  
for that to be in the layout header. I don’t see any reason why  
putting it in the body wont work, but worth checking. Also, since you  
are not loading the js library as standard for all pages, make sure it  
has loaded correctly for this page by looking at the page source etc.  
It may just be that the scriptaculous function is not getting called  
for some reason.

Since there is nothing out of the ordinary in the auto complete fields  
you are creating, it may be that there is a problem with the search  
that auto complete for is doing that is causing a database/rails error

- you would see that in the log.

Hope this may be of help  
Tonypm

---

<div class="post-metadata">

**Author:** ![damien](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de053/32.png) [@damien](https://www.ruby-forum.com/u/damien)\
**Post date:** [January 4, 2008, 10:53am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-problem/124983/6 "2008-01-04T10:53:27Z")

</div>

Sorry about my mistake. I posted back to Ryan but not into the general  
discussion that I’ve already tried the that guide but the CSS  
information which is generated in the rendered page is identical to  
the CSS in that guide.

I also double-checked the “name” field to make sure it is type “text”.

---

<div class="post-metadata">

**Author:** ![damien](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de053/32.png) [@damien](https://www.ruby-forum.com/u/damien)\
**Post date:** [January 7, 2008, 11:56am UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-problem/124983/7 "2008-01-07T11:56:04Z")

</div>

On Jan 7, 2:02 am, tonypm [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. the controller method accesses the model to do the search, formats  
> You will get valuable help from Firefox-Firebug extension and the  
> generated and see that the url etc makes sense. You dont need to be a
> 
> Since there is nothing out of the ordinary in the auto complete fields  
> you are creating, it may be that there is a problem with the search  
> that auto complete for is doing that is causing a database/rails error
> 
> - you would see that in the log.
> 
> Hope this may be of help  
> Tonypm

Thanks Tony! It turned out I had a myriad of problems.

The first being that for some reason just installing the plugin is not  
enough… I needed to add

\<%= javascript\_include\_tag “prototype”, “effects”, “controls” %\>  
\<%= javascript\_include\_tag “scriptaculous” %\>

to my view.

On top of that, I started getting a  
“ActionController::InvalidAuthenticityToken” which I found through  
firebug. Apparently this is some sort of feature in  
protects\_from\_forgery. Since my site doesn’t require authentication  
(it’s just a big free database) I placed

self.allow\_forgery\_protection = false

in my apps\>controllers\>application.rb file to turn it off.

Now that all that’s done it finally works and I can breathe a little  
easier. Best of luck to anyone with the same problem.

---

<div class="post-metadata">

**Author:** ![damien](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9de053/32.png) [@damien](https://www.ruby-forum.com/u/damien)\
**Post date:** [January 18, 2008, 2:52pm UTC](https://www.ruby-forum.com/t/text-field-with-auto-complete-problem/124983/8 "2008-01-18T14:52:19Z")

</div>

You make it in a wrong way…

Sending requests (for autocompleting results) via POSTs in RESTful app  
and disabling forgery\_protection isn’t good solution IMO.

You must force javascript to send request via GETs not POSTs.  
you can make it by adding {:method =\> :get} option, i.e:

\<%= text\_field\_with\_auto\_complete :Error, :name, {}, {:method  
=\> :get} %\>

In routes.rb you just add:

map.resources :solutions, :collection =\>  
{:auto\_complete\_for\_error\_name =\> :get }

more here:

[http://trix.pl/blog/ruby-on-rails/auto-complete-for-rails-2-0-tutorial](http://trix.pl/blog/ruby-on-rails/auto-complete-for-rails-2-0-tutorial)
