# Problem with Multi-Level Partials and observe\_field Updates

**URL:** <https://www.ruby-forum.com/t/problem-with-multi-level-partials-and-observe-field-updates/134557>\
**Category:** Rails\
**Created:** [April 12, 2008, 12:00am UTC](https://www.ruby-forum.com/t/problem-with-multi-level-partials-and-observe-field-updates/134557 "2008-04-12T00:00:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philip\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c5a1d2/32.png) [@Philip\_N](https://www.ruby-forum.com/u/Philip_N)\
**Post date:** [April 12, 2008, 12:00am UTC](https://www.ruby-forum.com/t/problem-with-multi-level-partials-and-observe-field-updates/134557/1 "2008-04-12T00:00:12Z")

</div>

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  
(removed\_email\_address@domain.invalid)
