A visitor to Ruby Zone asked in a comment about my post "Rails is cool":
Here is a step by step how I do it:
For testing purpose I'll create 2 tables: users and states
So, let's create the first table:
We insert some data:
We create the second table:
We insert some data:
Let's create a rails project and generate controllers and models for our tables:
We edit the user controller:
What are the 2 first lines in this view?
You need to get the very latest version of the Scriptaculous script files from http://script.aculo.us/downloads and to include the prototype.js and scriptaculous.js scripts in your view.
Open the zipped archive, take all javascript files and put them in the public/javascripts/scriptaculous folder
Now, point your browser to: http://localhost:3000/user/
You'll get a input box with auto_completing feature.
If you want to get input box with auto_completing feature that shows other field from other model, just do:
- Change in the controller this line: auto_complete_for :state, :name
with this line: auto_complete_for :your_model, :your_field
- Change in the index.rhtml file this line:
<%= text_field_with_auto_complete :state, :name , :skip_style => true %>
with:
<%= text_field_with_auto_complete :your_model, :your_field, :skip_style => true %>
That's all. Enjoy!
I was wondering how you could accomplish this with a different controller. For ex:
in the users view I have:
<%= text_field_with_auto_complete :state, :name %>
where would I put the controller code, in the user or state controller. so far both havent worked for me. Thanks
Here is a step by step how I do it:
For testing purpose I'll create 2 tables: users and states
So, let's create the first table:
mysql> create table users(
-> id int not null auto_increment,
-> name varchar(20),
-> primary key(id));
Query OK, 0 rows affected (0.09 sec)
We insert some data:
mysql> insert into users (name) values ("ab1"), ("ab2"), ("bb1"), ("bb2"),
("bb3"), ("cb1"), ("cb2"), ("cb3"), ("cb4");
Query OK, 9 rows affected (0.03 sec)
Records: 9 Duplicates: 0 Warnings: 0We create the second table:
mysql> create table states(
-> id int not null auto_increment,
-> name varchar(20),
-> primary key(id));
Query OK, 0 rows affected (0.06 sec)
We insert some data:
mysql> insert into states (name) values ("astate1"),("astate2"),
("bstate1"),("bstate2"),("bstate3");
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0Let's create a rails project and generate controllers and models for our tables:
>rails my_project
>cd my_project
my_project>ruby script/generate controller user
my_project>ruby script/generate controller state
my_project>ruby script/generate model user
my_project>ruby script/generate model state
We edit the user controller:
my_project>vi app/controllers/user_controller.rbWe edit the index view:
class UserController < ApplicationController
auto_complete_for :state, :name
end
my_project>vi app/views/user/index.rhtml
<%= javascript_include_tag "scriptaculous/prototype" %>
<%= javascript_include_tag "scriptaculous/scriptaculous" %>
<%= text_field_with_auto_complete :state, :name , :skip_style => true %>
What are the 2 first lines in this view?
You need to get the very latest version of the Scriptaculous script files from http://script.aculo.us/downloads and to include the prototype.js and scriptaculous.js scripts in your view.
Open the zipped archive, take all javascript files and put them in the public/javascripts/scriptaculous folder
Now, point your browser to: http://localhost:3000/user/
You'll get a input box with auto_completing feature.
If you want to get input box with auto_completing feature that shows other field from other model, just do:
- Change in the controller this line: auto_complete_for :state, :name
with this line: auto_complete_for :your_model, :your_field
- Change in the index.rhtml file this line:
<%= text_field_with_auto_complete :state, :name , :skip_style => true %>
with:
<%= text_field_with_auto_complete :your_model, :your_field, :skip_style => true %>
That's all. Enjoy!
27 Feb 2006 09:26:17
Thank you, thank you, thank you.
21 Apr 2006 12:34:37
great tutorial, thanks
22 May 2006 13:19:29
How can I autocomplete an ID?
24 Jun 2006 00:25:22
absolutely fantastic, conscientious, well produced, carefully explained, a great help.
27 Jun 2006 04:45:16
Hi,
I have a Customer table with first_name and last_name as 2 separate columns in the database.
Is there a way to have the autocomplete show the first_name + " " + last_name in the drop down list that pops up when the first few characters of the first name are typed in?
I appreciate your response.
Thank you.
20 Oct 2006 06:36:35
Thank you, thank you. I read at least 3 other tutorials but yours was the best, taking me through from start to finish.
24 Jul 2007 12:24:02
its not working.. .please explain me clearly.
08 Nov 2007 11:30:42
Autocomplete don't work when adding conditions. For example
auto_complete_for :server, :name, {:conditions => {:removed => false}}
Autocomplete will offer me all non removed servers but always. No matter what I type in search box.
08 Mar 2008 22:16:53
nice and simple, the rails way
27 Sep 2008 18:52:50
Ok,thank you for sharing!
It's a nice post!