text_field_with_auto_complete does not actually auto complete

61 views
Skip to first unread message

damien

unread,
Jan 7, 2008, 3:26:00 AM1/7/08
to Ruby on Rails: Spinoffs
I'm using Rails 2.0 and installed the plug-in for auto-complete.

The page shows up fine... the problem is that the
text_field_with_auto_complete in the view doesn't actually auto-
complete. Since I hand populated the database I know what to search
for, but it just won't show me any suggestions.

If someone might be able to point out what I'm doing wrong I'd greatly
appreciate it. Below are some code snippets from my very basic
project.

#VIEW _search.html.erb
<H3>Search for Your Error Message Below</H3>

<%= text_field_with_auto_complete :solution, :error_name %>

#VIEW index.html.erb
<H1>Welcome to the Solutions Lookup Database</H1>

<%= render :partial => 'search' %>

#CONTROLLER solutions_controller.rb
class SolutionsController < ApplicationController
auto_complete_for :solution, :error_name
def index

end
def search
:partial
end
end

#MIGRATION 002_testpopulationsolutions.rb
class Testpopulatesolutions < ActiveRecord::Migration
def self.up
Solution.create :error_name => "404", :description => "Website could
not be found.", :solution_steps => "Contact admin."
Solution.create :error_name => "ip conflict", :description => "Your
computer has the same network address as another.", :solution_steps =>
"Contact admin."
Solution.create :error_name => "computer restarts", :description =>
"Computer is most likely overheating.", :solution_steps => "Blow out
vents with compressed duster."
end

def self.down
drop_table :solutions
create_table :solutions do |t|
t.column :error_name, :string
t.column :description, :text
t.column :solution_steps, :text
end
end
end

Nicolás Sanguinetti

unread,
Jan 7, 2008, 9:06:02 AM1/7/08
to rubyonrail...@googlegroups.com
Do you have <%= javascript_include_tag :defaults %> in your layout
inside the <head> tag?

Best,
-Nicolas

damien

unread,
Jan 7, 2008, 2:57:06 PM1/7/08
to Ruby on Rails: Spinoffs
No, I didn't have that tag. It turns out though that using the <%=
javascript_include_tag :defaults %> works outside the header (just in
case you wanted to know).

I also found out that to defend against sessions fixation attacks,
rails uses a setting called allow_forgery_detection which was causing
problems with the POST data for the text_field_with_auto_complete,
giving me a "ActionController::InvalidAuthenticityToken" error message
which I was able to find using the Firebug plugin for Firefox. Since
my project doesn't require any authentication (it's just a giant free
database) I just added the line

self.allow_forgery_protection = false

into my application.rb file. Everything works now as it should. Thanks
for the help!

On Jan 7, 6:06 am, "Nicolás Sanguinetti" <godf...@gmail.com> wrote:
> Do you have <%= javascript_include_tag :defaults %> in your layout
> inside the <head> tag?
>
> Best,
> -Nicolas
>

Nicolás Sanguinetti

unread,
Jan 7, 2008, 3:23:54 PM1/7/08
to rubyonrail...@googlegroups.com
On Jan 7, 2008 5:57 PM, damien <Mike.P...@gmail.com> wrote:
>
> No, I didn't have that tag. It turns out though that using the <%=
> javascript_include_tag :defaults %> works outside the header (just in
> case you wanted to know).

It will work anywhere, as long as it's before the
text_field_with_auto_complete tag.

Best,
-Nicolas

Tomek

unread,
Jan 18, 2008, 8:48:51 AM1/18/08
to Ruby on Rails: Spinoffs
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 :solution, :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
Reply all
Reply to author
Forward
0 new messages