# How to deal with observe\_field in partials?

**URL:** <https://www.ruby-forum.com/t/how-to-deal-with-observe-field-in-partials/139038>\
**Category:** Rails\
**Created:** [May 30, 2008, 10:56am UTC](https://www.ruby-forum.com/t/how-to-deal-with-observe-field-in-partials/139038 "2008-05-30T10:56:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![enduro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@enduro](https://www.ruby-forum.com/u/enduro)\
**Post date:** [May 30, 2008, 10:56am UTC](https://www.ruby-forum.com/t/how-to-deal-with-observe-field-in-partials/139038/1 "2008-05-30T10:56:01Z")

</div>

Hiyas

I’m having a little problem with a observe\_field in a partial  
“bokings/room\_list”:

\<%= select\_tag “room\_#{room.id}_grownups", options\_for\_select([0, 1, 2],  
room.grownups.length) %\>  
\<%= observe\_field "room_#{room.id}\_grownups”,  
:url =\> set\_number\_of\_grownups\_booking\_room\_url(:id =\> room.id),  
:with =\> “‘number=’ + value”  
%\>

The set\_number\_of\_grownups action does the job and then updates the  
partial:

render :update do |page|  
page[‘room\_list’].replace\_html :partial =\> “bookings/room\_list”  
end

The first time, everything works just fine. But subsequent changes of  
the select fire the observer twice - at least to AJAX calls show up in  
Firebug. (It happens with Safari, too.)

Maybe the old observer is still sitting around and gets a twin once the  
partial is re-rendered. Strange though, no matter how often you change  
that select, the number of fired AJAX calls is always 2. That’s on Rails  
2 with Prototype 1.6.

Any ideas?

---

<div class="post-metadata">

**Author:** ![enduro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@enduro](https://www.ruby-forum.com/u/enduro)\
**Post date:** [May 30, 2008, 11:49am UTC](https://www.ruby-forum.com/t/how-to-deal-with-observe-field-in-partials/139038/2 "2008-05-30T11:49:35Z")

</div>

> render :update do |page|  
> page[‘room\_list’].replace\_html :partial =\> “bookings/room\_list”  
> end
> 
> The first time, everything works just fine. But subsequent changes of  
> the select fire the observer twice - at least to AJAX calls show up in  
> Firebug. (It happens with Safari, too.)
> 
> Maybe the old observer is still sitting around and gets a twin once the  
> partial is re-rendered. Strange though, no matter how often you change  
> that select, the number of fired AJAX calls is always 2. That’s on Rails  
> 2 with Prototype 1.6.

Yes, could be. And since there is no way to assign the prototype  
observer to a js variable, you can’t disable it before the call. Most  
likely it’s the old observer triggered by the replace and then replaced  
itself, so they don’t stack up infinitely, but get fired twice.

For you specific problem i don’t see, why you want to re-render the  
whole select tag, since it’s options are constant anyway.

Worth a try may be splitting the partial and not reinstalling the  
observer.

The hard way: Look at the generated code, take it as sample to write  
your own observer, assigned to a variable name, so you can disable it  
until the ajax response installs a new one.

---

<div class="post-metadata">

**Author:** ![enduro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@enduro](https://www.ruby-forum.com/u/enduro)\
**Post date:** [May 30, 2008, 1:02pm UTC](https://www.ruby-forum.com/t/how-to-deal-with-observe-field-in-partials/139038/3 "2008-05-30T13:02:38Z")

</div>

By the way:

If it’s for select boxes only, you really don’t need an observer. You  
can get similar results by using remote\_function together with the  
onchange event.

observe\_field is more interesting for text fields where you would have  
to hook into the keyevents to get similar results.

---

<div class="post-metadata">

**Author:** ![enduro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@enduro](https://www.ruby-forum.com/u/enduro)\
**Post date:** [May 30, 2008, 1:52pm UTC](https://www.ruby-forum.com/t/how-to-deal-with-observe-field-in-partials/139038/4 "2008-05-30T13:52:06Z")

</div>

Thorsten M. wrote:

> By the way:
> 
> If it’s for select boxes only, you really don’t need an observer. You  
> can get similar results by using remote\_function together with the  
> onchange event.
> 
> observe\_field is more interesting for text fields where you would have  
> to hook into the keyevents to get similar results.

Great, that’s the solution - and works like a charm with almost no  
changes at all.

Thanks! -sven

---

<div class="post-metadata">

**Author:** ![enduro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@enduro](https://www.ruby-forum.com/u/enduro)\
**Post date:** [May 30, 2008, 12:09pm UTC](https://www.ruby-forum.com/t/how-to-deal-with-observe-field-in-partials/139038/5 "2008-05-30T12:09:40Z")

</div>

Thorsten M. wrote:

> For you specific problem i don’t see, why you want to re-render the  
> whole select tag, since it’s options are constant anyway.

There are more selects in the partial, in fact, our client has a rather  
frowning matrix of dependencies between those selects which made it a  
more realistic approach to just update the entire partial whenever any  
select is changed.

Creating the observer by a hand-made helper certainly sounds better than  
my own plan B:

(Move the observers out of the partial and then make sure all selects  
are always present (yet hidden if necessary). One of the major browsers  
(dont remember if it’s FF or IE) though will submit form fields  
eventhough they are wripped by a display-none block element. Kinda  
nasty, but a on-submit JS could remove those from the DOM beforehand.)

Strange that this is not a more common problem.
