Problem with Multi-Level Partials and observe_field Updates

Folks,

I’m building a complex data entry / edit screen (to a customer spec) and
am
having problems with part of it.

I render a partial for line items in an order -

<%= render :partial => ‘order_product’, :collection =>
@order.order_products %>

This partial contains a text box to enter a product code. When a code
is
entered and you tab away from it (onblur) then it has to look up the
database
for the product text. Here’s the partial code which does this -

<% if order_product.new_record? %> <%= f.text_field :product_code, :size => 10, :value => '', :onblur => 'setProductRowID(this);RecalculateOrder();' %> <% else %> <%= f.text_field :product_code, :size => 10, :onblur => 'RecalculateOrder();' %> <% end %> <% product_code_field = numprefix + 'product_code' if order_product.new_record? opid = '' else opid = order_product.id.to_s end %> <%= observe_field product_code_field, :update => 'product_details', :url => {:controller => 'orders', :action => 'find_base_product' }, :with => "'product_code='+encodeURIComponent(value)+'&noe=#{new_or_existing}&pid=#{opid}'" %>

<% if params[:action] == ‘new’
p_c = ‘’
p_id = ‘’
else
if new_or_existing == ‘new’
p_c = ‘’
p_id = ‘’
else
p_c = order_product.product_code
p_id = order_product.id.to_s
end
end
%>

<%= render :partial => "product_details", :locals => {:product_code => p_c, :noe => new_or_existing, :pid => p_id } %>

The “product_details” partial is actually more complex than just a
simple
description field, but that will suffice for the explanation. The
problem is
(and thanks if you are still will be at this point) that sometimes the
description gets updated on the right “row” and other times the first
line
item row (or the bottom row if the row being entered isn’t the last one)
in
the table gets updated.

The find_base_product method in the controller is quite simple -

def find_base_product
local_pc = params[:product_code]
local_noe = params[:noe]
local_pid = params[:pid]
render :partial => ‘product_details’,
:layout => false,
:locals => { :product_code => local_pc,
:noe => local_noe,
:pid => local_pid}
end

Here’s what I have observed. It seems to occur on “edit” not on
“new”. It
happens with the first row added after the existing rows retrieved from
the
DB only. If it is the last row in the line item list, then it will
update
the text on the first row. If you add other line items and fill in the
product code from them it works. If you then go back to the “bad” row
and
try again, then it will update the last row in the list. Additionally
if
you add multiple rows without entering product codes all off then are
bad.

I’m totally confused !!!

Oh, we add new rows using the following in the main partial -

<%= add_order_product_link “Add an Item (F1)” %>

This is a helper method using pretty standard RJS -

def add_order_product_link(name)
link_to_function(name, nil, :id => ‘f1_link’) do |page|
page.insert_html :bottom, :order_products, :partial
=> ‘order_product’, :object => OrderProduct.new
end
end

All help gladly received !!!

Phil


Philip N.
ScotDB Limited
([email protected])