Is there any way to have observe_field react to the current value of a field also?

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 [email protected]

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 %>

HI Bob,

On Mon, Jun 28, 2010 at 3:16 PM, Bob S. [email protected] 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

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

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

Hi Bob,

On Tue, Jun 29, 2010 at 12:28 PM, Bob S. [email protected]
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