# Is there any way to have observe\_field react to the current value of a field also?

**URL:** <https://www.ruby-forum.com/t/is-there-any-way-to-have-observe-field-react-to-the-current-value-of-a-field-also/190831>\
**Category:** Rails\
**Created:** [June 28, 2010, 10:18pm UTC](https://www.ruby-forum.com/t/is-there-any-way-to-have-observe-field-react-to-the-current-value-of-a-field-also/190831 "2010-06-28T22:18:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [June 28, 2010, 10:18pm UTC](https://www.ruby-forum.com/t/is-there-any-way-to-have-observe-field-react-to-the-current-value-of-a-field-also/190831/1 "2010-06-28T22:18:12Z")

</div>

I have a view, seen below, that shows a list of churches if the zip  
code isn’t 01540 or 01537. This part works perfectly. The problem is  
that this is the page is to edit an existing record also, so  
observe\_field needs to see that the zip code currently doesn’t match,  
so the church list needs to be shown. Currently, even if the zip  
doesn’t match, the list will only be shown after the field is changed,  
even is the field is changed to the same value… Here is the view,  
controller code and church\_select view in that order… Please give me  
a direction to look…

Thanks

Bob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

View

\<% fields\_for @household, :builder =\> TaggedBuilder do |  
household\_form| %\>

```
\<%= household\_form.text\_field :first\_name, :maxlength =\> 50, :size =\> 15, :autocomplete =\> "off", :class =\> 'household ' %\> \<%= household\_form.text\_field :middle, :size =\> 1, :maxlength =\>1, :autocomplete =\> "off", :class =\> 'household ' %\> \<%= household\_form.text\_field :last\_name, :maxlength =\> 50, :size =\> 20, :autocomplete =\> "off", :class =\> 'household ' %\> \<%= household\_form.text\_field :address, :autocomplete =\> "off", :class =\> 'household ' %\> \<%= household\_form.text\_field :zip, :size=\> 5, :autocomplete =\> "off", :class =\> 'household ' %\> \<%= household\_form.text\_field number\_to\_phone(:phone, :area\_code =\> true), :autocomplete =\> "off", :size =\> 14, :class =\> 'household ' %\>
<%= observe_field :household_zip,
          :url => {:controller => 'households', :action => 
'watch_church'},
          :update => :church,
          :with => ':zip',
          :frequency=> 0.5 %>

**Federal Programs**** Primary Income** <%=
household_form.select(:primary, [['1. Employment', 1],['2.
Unemployment', 2],['3. SSI', 4],['4. TANF/EADC', 8],['5. Other', 16],
['6. None', 32]], :class => "household ") %>

\<% end %\>

controller  
def watch\_church  
# debugger  
if (params[‘:zip’] != “01540”) && (params[‘:zip’] != “01537”)  
then  
render :partial =\> “church\_select”  
else  
render(:nothing=\>true)  
end  
end

church\_select view  
**Member of Local Church:** \<%= collection\_select(‘household’,  
‘church\_id’, Church.find(:all), ‘id’, ‘name’) %\>
```

| \<%= household\_form.check\_box :food\_stamps %\> |
| \<%= household\_form.check\_box :wic %\> |
| \<%= household\_form.check\_box :school\_breakfast %\> |
| \<%= household\_form.check\_box :school\_lunch %\> |
| \<%= household\_form.check\_box :SFSD %\> |

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [June 29, 2010, 1:58pm UTC](https://www.ruby-forum.com/t/is-there-any-way-to-have-observe-field-react-to-the-current-value-of-a-field-also/190831/2 "2010-06-29T13:58:16Z")

</div>

HI Bob,

On Mon, Jun 28, 2010 at 3:16 PM, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The problem is  
> that this is the page is to edit an existing record also, so  
> observe\_field needs to see that the zip code currently doesn’t match,  
> so the church list needs to be shown. Currently, even if the zip  
> doesn’t match, the list will only be shown after the field is changed,

For the initial page load you just need to test and render if  
appropriate on the initial page load.

 \<% if @household.zip != xxxxx and @household.zip != yyyyy %\> \<%= render :partial =\> "church\_select %\> \<% end %\>

You need to change the render :nothing in your controller to render an  
empty string or partial to empty the content of the church div when  
appropriate.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [June 29, 2010, 7:28pm UTC](https://www.ruby-forum.com/t/is-there-any-way-to-have-observe-field-react-to-the-current-value-of-a-field-also/190831/3 "2010-06-29T19:28:36Z")

</div>

How would this work with the observe\_field later in the page ? This is  
an edit page, so if the field is changed, the page needs to react  
correctly This works fine for a new user, but will it be OK for changing  
an existing user ?

Thanks for the reply

Bob

> \<% if @household.zip != xxxxx and @household.zip != yyyyy %\> \<%= render :partial =\> "church\_select %\> \<% end %\>
> 
> You need to change the render :nothing in your controller to render an  
> empty string or partial to empty the content of the church div when  
> appropriate.
> 
> HTH,  
> Bill

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [June 29, 2010, 7:54pm UTC](https://www.ruby-forum.com/t/is-there-any-way-to-have-observe-field-react-to-the-current-value-of-a-field-also/190831/4 "2010-06-29T19:54:18Z")

</div>

Never mind. I just tried it and it works perfectly. Thanks for the help.

Bob

Bob S. wrote:

> How would this work with the observe\_field later in the page ? This is  
> an edit page, so if the field is changed, the page needs to react  
> correctly This works fine for a new user, but will it be OK for changing  
> an existing user ?
> 
> Thanks for the reply
> 
> Bob
> 
> > \<% if @household.zip != xxxxx and @household.zip != yyyyy %\> \<%= render :partial =\> "church\_select %\> \<% end %\>
> > 
> > You need to change the render :nothing in your controller to render an  
> > empty string or partial to empty the content of the church div when  
> > appropriate.
> > 
> > HTH,  
> > Bill

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [June 29, 2010, 7:41pm UTC](https://www.ruby-forum.com/t/is-there-any-way-to-have-observe-field-react-to-the-current-value-of-a-field-also/190831/5 "2010-06-29T19:41:56Z")

</div>

Hi Bob,

On Tue, Jun 29, 2010 at 12:28 PM, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> How would this work with the observe\_field later in the page ? This is  
> an edit page, so if the field is changed, the page needs to react  
> correctly This works fine for a new user, but will it be OK for changing  
> an existing user ?

That’s what the second part, below, is about.

> > You need to change the render :nothing in your controller to render an  
> > empty string or partial to empty the content of the church div when  
> > appropriate.

Best regards,  
Bill
