# Help with observe\_field please!

**URL:** <https://www.ruby-forum.com/t/help-with-observe-field-please/88625>\
**Category:** Rails\
**Created:** [March 13, 2007, 5:33am UTC](https://www.ruby-forum.com/t/help-with-observe-field-please/88625 "2007-03-13T05:33:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jorge\_Mario\_GSMazo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71c47a/32.png) [@Jorge\_Mario\_GSMazo](https://www.ruby-forum.com/u/Jorge_Mario_GSMazo)\
**Post date:** [March 13, 2007, 5:33am UTC](https://www.ruby-forum.com/t/help-with-observe-field-please/88625/1 "2007-03-13T05:33:10Z")

</div>

Hi there I’m observing a field (collection\_select) but if the  
collection only has one item, the observe field will not work, it  
works for more than one item  
here is my code  
\<%= collection\_select ‘address’, ‘address’, @addresses, :id, :address  
%\>

  
\<%= observe\_field(  
:address\_address,  
:update =\> ‘shipping’,  
:url =\> { :action =\> :shi },  
:on =\> “onselect”,  
:complete =\> “new Effect.SlideDown(‘shipping’)”,  
:with =\> ‘boo’) %\>

I have tried a different of bunch :on =\> event  
but no efect  
any help would be very aprreciated

---

<div class="post-metadata">

**Author:** ![Jorge\_Mario\_GSMazo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71c47a/32.png) [@Jorge\_Mario\_GSMazo](https://www.ruby-forum.com/u/Jorge_Mario_GSMazo)\
**Post date:** [March 13, 2007, 12:27pm UTC](https://www.ruby-forum.com/t/help-with-observe-field-please/88625/2 "2007-03-13T12:27:56Z")

</div>

Hi Jorge,

Jorge Mario G. Mazo wrote:

> Hi there I’m observing a field (collection\_select) but if the  
> collection only has one item, the observe field will not work

That’s because, with only one item in the collection, there’s no event  
occurring. I think you’ll have to force the event by using a blank  
item,  
maybe with ‘select one’ for the text at the beginning of the collection.  
Check the option\_from\_collection\_for\_select documentation. You may need  
to  
modify your approach a little. It says ‘you have to wrap this call in a  
regular HTML select tag.’

Another option would be to render your UI differently depending on the  
number of items in the collection.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![Jorge\_Mario\_GSMazo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71c47a/32.png) [@Jorge\_Mario\_GSMazo](https://www.ruby-forum.com/u/Jorge_Mario_GSMazo)\
**Post date:** [March 13, 2007, 1:45pm UTC](https://www.ruby-forum.com/t/help-with-observe-field-please/88625/3 "2007-03-13T13:45:07Z")

</div>

On Mar 13, 2007, at 12:31 AM, Jorge Mario G. Mazo wrote:

> ```
> :url => { :action => :shi },
> :on => "onselect",
> :complete => "new Effect.SlideDown('shipping')",
> :with => 'boo') %>
> 
> ```
> 
> I have tried a different of bunch :on =\> event  
> but no efect  
> any help would be very aprreciated

To save you a dive down into the prototype.js library (since I was  
just there a week ago ;-), the only event that the underlying  
JavaScript supports is an implicit ‘change’ or ‘click’ depending on  
the element type (click for radio buttons and checkboxes, change for  
others).

I spent several hours tracking down odd behavior which was partly  
explained by this. (The other part was having an autocomplete and  
and observe-field on the same form element – nasty timing issues  
there!)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
