# Correct use of :complete and :before on a text\_field\_with\_au

**URL:** <https://www.ruby-forum.com/t/correct-use-of-complete-and-before-on-a-text-field-with-au/86046>\
**Category:** Rails\
**Created:** [February 19, 2007, 10:25am UTC](https://www.ruby-forum.com/t/correct-use-of-complete-and-before-on-a-text-field-with-au/86046 "2007-02-19T10:25:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 19, 2007, 10:25am UTC](https://www.ruby-forum.com/t/correct-use-of-complete-and-before-on-a-text-field-with-au/86046/1 "2007-02-19T10:25:04Z")

</div>

Hey guys!

Having a problem here figuring out how to use my :before and :complete  
statements in a text\_field\_with\_auto\_complete construct.

## At the moment, I have this bit of code within an edit.rhtml file:

```
  <%= f.text_field 'publisher_id', :size => 7 %>
  <%= observe_field(:item_publisher_id,
                    :frequency => 0.1,
                   # :update => :publisher_name,
                    :before =>

```

## “Element.show(‘publisher\_background\_activity’)”, :complete =\> “Element.hide(‘publisher\_background\_activity’)”, :url =\> { :action =\> :auto\_complete\_publisher\_name\_for\_id }, :with =\> “‘id=’ + encodeURIComponent(value)” ) -%\> \<%= text\_field\_with\_auto\_complete :publisher, :name, { :value =\> @item.publisher.name, :size =\> 60 }, { :frequency =\> 0.5, :before =\> “Element.show(‘publisher\_background\_activity’)”, :complete =\> “Element.hide(‘publisher\_background\_activity’)”, :after\_update\_element =\> ‘auto\_complete\_on\_select’, :min\_chars =\> 2 } -%\> …

Basically, this creates two input fields, one where you can put in an  
ID of a publisher, and another, where you can write text, and AJAX is  
used to fetch publishers whose names contain that text.

As it is stands, everything works like expected, except for the  
showing and hiding of my span which indicates that something’s  
happening behind the scenes. It works on the first input field which  
uses observe\_field - it shows when the user inputs something and  
disappears once the result is returned. But on the  
text\_field\_with\_auto\_complete, the div is not shown and hidden before  
the moment you select one of the items on the list that is returned by  
the AJAX call. I’d like for it to be shown once the call to lookup  
names in the database is initiated, and to be hidden once the list is  
delivered to the browser.

How do I do that?

Thanks in advance,

Daniel Buus

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 19, 2007, 11:58am UTC](https://www.ruby-forum.com/t/correct-use-of-complete-and-before-on-a-text-field-with-au/86046/2 "2007-02-19T11:58:22Z")

</div>

Actually, I believe the reason it shows and hides the div at all has  
nothing to do with the statements in the auto\_complete field, but in  
the fact that the ID input field is updated as a result of the  
auto\_complete field, which fires the ID field’s AJAX operations. So,  
the :before and :completes on the auto\_complete field do nothing,  
really.

On Feb 19, 10:24 am, “Daniel Smedegaard B.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 19, 2007, 12:06pm UTC](https://www.ruby-forum.com/t/correct-use-of-complete-and-before-on-a-text-field-with-au/86046/3 "2007-02-19T12:06:24Z")

</div>

Okay, forget it. Found the :indicator property. It really bugs me that

a) The consistency between properties and methods on different AJAX-  
based form helpers is shabby to say the least  
b) The API documentation on this part of Rails is so sparse. You have  
to skate around between different classes, and often you don’t find  
the right information for that particular helper derived from some  
parent class, like in this example, where Google finally served me a  
page with a description of the :indicator property.

Hm! 🙂

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 19, 2007, 12:06pm UTC](https://www.ruby-forum.com/t/correct-use-of-complete-and-before-on-a-text-field-with-au/86046/4 "2007-02-19T12:06:24Z")

</div>

On Feb 19, 2007, at 10:24 AM, Daniel Smedegaard B. wrote:

> Basically, this creates two input fields, one where you can put in an  
> ID of a publisher, and another, where you can write text, and AJAX is  
> used to fetch publishers whose names contain that text.

Just for the record, are you aware of the plugin model\_auto\_completer?

[http://agilewebdevelopment.com/plugins/model\_auto\_completer](http://agilewebdevelopment.com/plugins/model_auto_completer)

? If you really need both text fields in the interface it won’t apply  
though.

– fxn

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 19, 2007, 1:48pm UTC](https://www.ruby-forum.com/t/correct-use-of-complete-and-before-on-a-text-field-with-au/86046/5 "2007-02-19T13:48:38Z")

</div>

On Feb 19, 12:05 pm, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ? If you really need both text fields in the interface it won’t apply  
> though.

Didn’t know about that one, no - thanks 🙂

But, as it turns out, I need the user to be able to manually type in  
the ID and get the corresponding name, and be able to type in a  
fragment of a name, then get the ID. Besides that, not all my primary  
keys are integers, so that plugin won’t work. This is a seriously  
specialized app 😉 But I can use that plug on other projects of mine,  
so thanks for the link!

Daniel

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 19, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/correct-use-of-complete-and-before-on-a-text-field-with-au/86046/6 "2007-02-19T14:13:46Z")

</div>

On Feb 19, 2007, at 1:47 PM, Daniel Smedegaard B. wrote:

> But, as it turns out, I need the user to be able to manually type in  
> the ID and get the corresponding name, and be able to type in a  
> fragment of a name, then get the ID.

OK, yeah the plugin does not help then.

> Besides that, not all my primary  
> keys are integers, so that plugin won’t work. This is a seriously  
> specialized app 😉 But I can use that plug on other projects of mine,  
> so thanks for the link!

Good. Note however that the plugin does not assume the key is an  
integer. The key is extracted with a regexp from the ID attribute of  
LIs. That regexp is “(\d+)$” by default (extract trailing integers),  
but is configurable.

– fxn

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 19, 2007, 3:09pm UTC](https://www.ruby-forum.com/t/correct-use-of-complete-and-before-on-a-text-field-with-au/86046/7 "2007-02-19T15:09:51Z")

</div>

On Feb 19, 2:12 pm, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

Ah okay, I just read the doc about trailing integers. Didn’t know it  
was configurable, but that’s even better then! 😃

Cheers
