# Observe\_field + enable/disable field

**URL:** <https://www.ruby-forum.com/t/observe-field-enable-disable-field/115380>\
**Category:** Rails\
**Created:** [September 24, 2007, 6:08pm UTC](https://www.ruby-forum.com/t/observe-field-enable-disable-field/115380 "2007-09-24T18:08:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karine\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e99b99/32.png) [@Karine\_D](https://www.ruby-forum.com/u/Karine_D)\
**Post date:** [September 24, 2007, 6:08pm UTC](https://www.ruby-forum.com/t/observe-field-enable-disable-field/115380/1 "2007-09-24T18:08:49Z")

</div>

Hello,

I have a form with two observe\_field, one on a select tag, the other on  
a checkbox. The form also has a text field. By default, the checkbox  
is not checked, the text field is disabled, and changing the select  
value will change the text field value. If the user checks the box,  
the text field is enabled, and the select doesn’t change it anymore.  
If unchecked, the text field is disabled again and the default  
behavior comes back.

So far, I have the text field value right, but not its  
enabling/disabling. I guess that’s just some simple Ajax I’m missing on  
the :complete option, but I can’t find the right way to do it as it  
depends on the checkbox state. Or should that go in the controller?  
(where I put my #TODO lines…)

Thanks,  
Karine D.

form partial:

Collection  
\<%= select('book', 'collection\_id', Collection.find(:all).collect {|c| [c.name, c.id]}) %\>

Note  
Give another note than the collection's one

\<%= render :partial =\> 'note' %\>

\<%= observe\_field ‘book\_collection\_id’,  
:update =\> “ajaxWrapper”,  
:with =\> “‘collection\_id=’ + encodeURIComponent(value) +  
‘&overwrite\_note=’ + escape($(‘book\_overwrite\_note’).checked)”,  
:url =\> {:controller =\> “user”, :action =\> ‘note’} %\>  
\<%= observe\_field ‘book\_overwrite\_note’,  
:update =\> “ajaxWrapper”,  
:with =\> “‘overwrite\_note=’ + encodeURIComponent(value) +  
‘&collection\_id=’ + escape($(‘book\_collection\_id’).value)”,  
:url =\> {:controller =\> “user”, :action =\> ‘note’} %\>

note partial:

controller:  
def note  
if (params[:overwrite\_note] == “on” || params[:overwrite\_note] ==  
“true”)  
#TODO: enable book\_note  
else  
@url\_landing = Collection.find(params[:collection\_id]).note  
#TODO: disable book\_note  
end  
render :partial =\> ‘note’, :layout =\> false  
end
