easy autocomplete in ActiveScaffold

103 views
Skip to first unread message

Robin

unread,
Oct 2, 2007, 11:36:59 PM10/2/07
to ActiveScaffold : Ruby on Rails plugin
Trying to create autocomplete fields in AS gave me some headaches, but
here is an easy way to make autocomplete text fields that fit right in
with AS default text fields.

In a form override partial (e.g., _#{columnname}_form_column.rhtml):

<dl>
<dt><label for="<%= "record_#{column.name}" %>"><%= column.label %></
label></dt>
<dd>
<%= text_field_with_auto_complete
active_scaffold_config.model.to_s.underscore, column.name, :class =>
'text-input'%>
<span class="description"><%= column.description %></span>
</dd>
</dl>

in the controller:

auto_complete_for :#{model_name}, :#{column_name}

For instance I have:

app/views/lab_resources/_cpu_form_column.rhtml with the above HTML/ERB
exactly and
app/controllers/lab_resources_controller with the line:

auto_complete_for :lab_resource, :cpu

CCH

unread,
Oct 3, 2007, 12:53:04 AM10/3/07
to ActiveScaffold : Ruby on Rails plugin
Hi Robin

Is there any way you can roll it into your own form.ui stuff ?

Robin

unread,
Oct 5, 2007, 6:12:00 AM10/5/07
to ActiveScaffold : Ruby on Rails plugin
Hmmm, now that would be cool. Haven't done it before. If the slave
drivers let me slow down a bit I'll give it a go ;)

On Oct 2, 11:36 pm, Robin <rdwhee...@rsglobal.net> wrote:
> Trying to createautocompletefields in AS gave me some headaches, but
> here is an easy way to makeautocompletetext fields that fit right in

CCH

unread,
Oct 5, 2007, 6:19:14 AM10/5/07
to ActiveScaffold : Ruby on Rails plugin
Hi Robin

Go for it !

I had issues with the text-input so much show that I have not
implemented my own

form.ui=:text_input support :-)

> > auto_complete_for :lab_resource, :cpu- Hide quoted text -
>
> - Show quoted text -

Antonio Bardazzi Multicore-Sistemi.com

unread,
Oct 5, 2007, 6:42:55 PM10/5/07
to ActiveScaffold : Ruby on Rails plugin
Don't seem AS save yours autocomplete fields.
I'm, workin on this:

<%
column_name = column.name
field_value = @record[column_name
field_name = "record[#{column_name}]"
field_id = "record_#{column_name}"
field_class = "#{column_name}-input text-input"
model_name = active_scaffold_config.model.to_s.underscore
autocomplete_id =
"record_#{column_name}_auto_complete_#{@record[:id]}"
autocomplete_url = url_for(:controller =>
model_name.pluralize, :action =>
"auto_complete_for_#{model_name}_#{column_name}")
autocomplete_param_name = "#{model_name}[#{column_name}]"
%>
<dl>
<dt><label for="<%=field_id%>"><%=column.label%></label></dt>
<dd>
<%=text_field_tag field_name, field_value, :id => field_id, :class
=> field_class%>
<div class="auto_complete <%=field_class%>" id="<%=autocomplete_id
%>"></div>
<%= javascript_tag("new Ajax.Autocompleter('#{field_id}',
'#{autocomplete_id}', '#{autocomplete_url}', {paramName:
'#{autocomplete_param_name}', fullSearch: true, frequency: 0,
minChars: 2})") -%>
</dd>
</dl>

But the autocomplete result don't get the class from own field

jon.bud...@gmail.com

unread,
Oct 6, 2007, 9:06:53 AM10/6/07
to ActiveScaffold : Ruby on Rails plugin
On Oct 2, 11:36 pm, Robin <rdwhee...@rsglobal.net> wrote:
> Trying to create autocomplete fields in AS gave me some headaches, but
> here is an easy way to make autocomplete text fields that fit right in
> with AS default text fields.
>
> [snip-a-dee-doo-dah]

Awesome, Robin!

This is 99% of something I was looking to do just yesterday.

The other 1% is limiting the auto-complete values based on the current
user. I've got a method which pulls out the values I want, and
ideally would like to pass that to the auto_complete list, but can't
figure out how to do that...

Ideas, anyone?

Thanks, in advance,
jmb-d

Robin

unread,
Oct 6, 2007, 11:16:10 AM10/6/07
to ActiveScaffold : Ruby on Rails plugin
Antonio, you are correct! The code as I originally wrote it won't
quite work: AS won't save the values because the text field has
id="#{model_name}_#{column_name} whereas AS wants
"record_#{column_name}". I;m working on a fix now and will post when
its done.

On Oct 5, 6:42 pm, "Antonio Bardazzi Multicore-Sistemi.com"

Antonio Bardazzi Multicore-Sistemi.com

unread,
Oct 6, 2007, 3:30:44 PM10/6/07
to ActiveScaffold : Ruby on Rails plugin
To specify config.columns[:wow_an_autocompleting_column].form_ui
= :autocomplete
copy this in ApplicationHelper:

def active_scaffold_input_autocomplete(column, options)


column_name = column.name
field_value = @record[column_name]
field_name = "record[#{column_name}]"
field_id = "record_#{column_name}"
field_class = "#{column_name}-input text-input"
model_name = active_scaffold_config.model.to_s.underscore
autocomplete_id =
"record_#{column_name}_auto_complete_#{@record[:id]}"
autocomplete_url = url_for(:controller =>
model_name.pluralize, :action =>
"auto_complete_for_#{model_name}_#{column_name}")
autocomplete_param_name = "#{model_name}[#{column_name}]"

tag = text_field_tag field_name, field_value, :id =>
field_id, :class => field_class
js = javascript_tag("new Ajax.Autocompleter('#{field_id}',


'#{autocomplete_id}', '#{autocomplete_url}', {paramName:
'#{autocomplete_param_name}', fullSearch: true, frequency: 0,
minChars: 2})")

"#{tag}<div class=\"auto_complete\" id=\"#{autocomplete_id}
\"></div>#{js}<span class=\"description\">#{column.description}</span>
</dd> </dl>"
end

It seems work but I don't know if has some side effects.

Message has been deleted

CCH

unread,
Oct 7, 2007, 12:30:49 AM10/7/07
to ActiveScaffold : Ruby on Rails plugin
Hi Antonio

On Oct 7, 3:30 am, "Antonio Bardazzi Multicore-Sistemi.com"


- Hide quoted text -
- Show quoted text -

cch: Did just that and added to one of my controllers for a text-
input
column

config.columns[:bill_no].form_ui = :autocomplete
save
restart server


Result : No error message but no auto-completion :-)


CCH
http://cch4rails.blogspot.com


On Oct 7, 3:30 am, "Antonio Bardazzi Multicore-Sistemi.com"

> > > But the autocomplete result don't get the class from own field- Hide quoted text -

Antonio Bardazzi Multicore-Sistemi.com

unread,
Oct 7, 2007, 5:52:11 AM10/7/07
to ActiveScaffold : Ruby on Rails plugin
Sorry I don't known ;)

Watch the log and debug with logger.info for see what is happening:
active_scaffold_input_autocomplete is called ?

Use firefox firebug->net params/response to see if your
auto_complete_for_#{model}_#{column_name} is called with right params

> CCHhttp://cch4rails.blogspot.com

CCH

unread,
Oct 9, 2007, 10:45:28 PM10/9/07
to ActiveScaffold : Ruby on Rails plugin
Hi Antonio

Do you have a simple project that shows how the auto-completion is
supposed to work ?
If so,can you upload it in the Files Section of this group?

TIA

On Oct 7, 5:52 pm, "Antonio Bardazzi Multicore-Sistemi.com"

> > > - Show quoted text -- Hide quoted text -

Open individual

unread,
Oct 19, 2007, 7:52:38 AM10/19/07
to ActiveScaffold : Ruby on Rails plugin
Has anyone got an autocomplete to work?

If so could you tell me what you did as I am trying everything in the
_form.rhtml override but with no success.

TIA

Open individual

unread,
Oct 19, 2007, 8:44:49 AM10/19/07
to ActiveScaffold : Ruby on Rails plugin
Hey CCH if you are out there can you help me.

I am really stuck as most examples of autocomplete on the web use the
url for an action and I can't do this with AS.

Open individual

unread,
Oct 19, 2007, 8:51:04 AM10/19/07
to ActiveScaffold : Ruby on Rails plugin
The problem with using Ruby's html generation directly is that <%=
text_field_with_auto_complete :product, :colour %> will not give the
control I need over the html formatting in the full override which I
desperately need.

I suppose I can still capture what is genrated in FireBug and work
backwards but I can't get autocomplete to work on even simple
examples.

eg "products" table and "colour" field

Open individual

unread,
Oct 20, 2007, 10:02:07 AM10/20/07
to ActiveScaffold : Ruby on Rails plugin
Not to worry after doing a full form override I got it to work.
I had a duplicate "prototype" javascript entry that threw me off and
was difficult to find that it was causing the problem.

Thanks


Open individual

unread,
Oct 20, 2007, 1:59:57 PM10/20/07
to ActiveScaffold : Ruby on Rails plugin
Dammit it is only working partially.

If I use the this in my _form.rhtml override for product then it
works.

<li class="form-element">
<dl>
<dt>
<label for="record_colour">Colour</label>
</dt>
<dd>


<%= text_field_with_auto_complete :product, :colour %>

</dd>
</dl>
</li>

and have

auto_complete_for :product, :colour

In the product controller.
However the id and name attributes that it generates are not desirable
as the AS obviously does not accept that naming convention.

Here is how one form element should look.

<li class="form-element">
<dl>
<dt>
<label for="record_price">Price</label>
</dt>
<dd>
<input type="text" autocomplete="on" class="price-input text-
input" id="record_price" name="record[price]" maxlength="15" size="15"
value=""/>
</dd>
</dl> </li>

But the <%= text_field_with_auto_complete :product, :colour %> ruby
statement is generating the code below.
I have typed in "B" and it correctly generates the auto-suggest area.

<li class="form-element">
<dl>
<dt>
<label for="record_colour">Colour</label>
</dt>
<dd>
<style type="text/css"> div.auto_complete {
width: 350px;
background: #fff;
}
div.auto_complete ul {
border:1px solid #888;
margin:0;
padding:0;
width:100%;
list-style-type:none;
}
div.auto_complete ul li {
margin:0;
padding:3px;
}
div.auto_complete ul li.selected {
background-color: #ffb;
}
div.auto_complete ul strong.highlight {
color: #800;
margin:0;
padding:0;
}
</style><input type="text" size="30" name="product[colour]"
id="product_colour" autocomplete="off"/><div
id="product_colour_auto_complete" class="auto_complete"
style="position: absolute; left: 584px; top: 529px; width: 206px;
opacity: 0.999999; display: none;"><ul><li class="selected">Blue</
li><li>Black</li><li>Brown</li></ul></div>
</dd>
</dl>


</li>


I have captured what it generates (basically the above code) and
modified it slightly to try and get and override so that I can format
the <input ...> tag.

This is my whole reason for doing so.

Can anyone tell me how to get this working as I am most frustrated and
have spend 3 days on it with no success.

Thanks for any help in advance.

Uncle Mike

Open individual

unread,
Oct 20, 2007, 2:51:11 PM10/20/07
to ActiveScaffold : Ruby on Rails plugin
Here is the very best autosuggest I have seen to date.
If AS can get something like this working I will be very happy.
Unfortunately some of this is in PHP.

Has anyone got an idea how I can get this autosuggest working with a
form override has I have tried using Firebug to copy the generated
code and paste it back into the _form.rhtml of product but this is not
working.

Please can someone out there help me in this moment of crises.

Open individual

unread,
Oct 21, 2007, 5:56:05 AM10/21/07
to ActiveScaffold : Ruby on Rails plugin
Here is the URL for the best autosuggest I have seen.

http://www.dcs.lancs.ac.uk/~grill/mgbox/

But it is in PHP.

Has anyone got the autosuggest working for a full form override?

TIA

Reply all
Reply to author
Forward
0 new messages