# Observers Don't Save State Across Form Submit \[Help\]

**URL:** <https://www.ruby-forum.com/t/observers-dont-save-state-across-form-submit-help/128652>\
**Category:** Rails\
**Created:** [February 9, 2008, 10:52pm UTC](https://www.ruby-forum.com/t/observers-dont-save-state-across-form-submit-help/128652 "2008-02-09T22:52:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kommissar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/48db29/32.png) [@kommissar](https://www.ruby-forum.com/u/kommissar)\
**Post date:** [February 9, 2008, 10:52pm UTC](https://www.ruby-forum.com/t/observers-dont-save-state-across-form-submit-help/128652/1 "2008-02-09T22:52:45Z")

</div>

Hello, any help with the issue below would be much appreciated!

I have a from that I use the observe\_field method on to “watch” a drop  
down box. Depending upon how the drop down box changes, I update the  
contents of a div with more form fields for the user to fill out. A  
small example, you can imagine a drop down box for the “Make” of a  
car. When the user selects “Honda” from this drop down box, the  
observer would update the contents of a div with another drop down box  
that asks for “Model” (so the user could choose “Civic” or something).

This works well when the user submits the form and it validates and  
saves, but when the form contains errors and _doesn’t_ validate, only  
the state of the drop down box is saved. The additional form fields  
that are triggered by that drop down are _not_ displayed.

To get it to show up, the user has to change the value of the drop  
down to something other than what it is, and then change it back.  
This is not what should be happening here.

Here is some code to try and make this more clear:

On the “new.html.erb” page I have this code:

\<%= collection\_select :app\_session\_parameter\_group, :attack\_type,

[“Select…”].concat(AppSessionParameterGroup.attack\_types), :to\_s,  
:to\_s  
%\>

\<%= observe\_field :app\_session\_parameter\_group\_attack\_type,  
:url =\> { :action =\> :attack\_type\_callback },  
:update =\> :attack\_type\_div,  
:with =\>  
“‘app\_session\_parameter\_group[attack\_type]=’+encodeURIComponent(value)”  
%\>

&nbsp;

When the user changes the “attack\_type” drop down box to “reboot”, the  
“attack\_type\_div” gets filled in with this:

\<%= text\_field\_tag ‘app\_session\_parameter\_group[probability]’,  
‘’, :size =\> 4 %\>

The user should then see this new div, and specify a value for  
probability. If they type in an invalid probability, like anything  
less than zero or greater than one, when they submit the page it fails  
to validate.

The problem is that when the page fails to validate, the drop down  
still says “reboot” but the “probability” form field isn’t there! To  
get it to show up, the user has to change the “attack\_type” to  
something other than “reboot” and then change it back.

Is there a way to fix this behavior so that when the page reloads the  
drop down box will still be there with the red error frame around it?
