Text_field_with_auto_complete problem

I can’t get the autocomplete feature of 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 %>
#######################

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

You could try what sandy posted here:

http://railsforum.com/viewtopic.php?pid=49557#p49557

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

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

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”.

On Jan 7, 2:02 am, tonypm [email protected] 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.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs