# Observing few fields in a form - Best Practise?

**URL:** <https://www.ruby-forum.com/t/observing-few-fields-in-a-form-best-practise/150671>\
**Category:** Rails\
**Created:** [October 11, 2008, 6:34am UTC](https://www.ruby-forum.com/t/observing-few-fields-in-a-form-best-practise/150671 "2008-10-11T06:34:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [October 11, 2008, 6:34am UTC](https://www.ruby-forum.com/t/observing-few-fields-in-a-form-best-practise/150671/1 "2008-10-11T06:34:53Z")

</div>

Hi,

im looking to build a page similar to a shopping cart page. When you  
change the ‘price’ or ‘quantity’ fields, the total is calculated and  
displayed.  
There are other fields in the form which have no relation to this  
action. So i guess observe\_form is not required. Can someone tell me  
what the best practise is for this?

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [October 11, 2008, 7:12am UTC](https://www.ruby-forum.com/t/observing-few-fields-in-a-form-best-practise/150671/2 "2008-10-11T07:12:53Z")

</div>

On Oct 11, 12:33 pm, Vinay [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> im looking to build a page similar to a shopping cart page. When you  
> change the ‘price’ or ‘quantity’ fields, the total is calculated and  
> displayed.  
> There are other fields in the form which have no relation to this  
> action. So i guess observe\_form is not required. Can someone tell me  
> what the best practise is for this?

Javascript on the client-side will give you the fastest response, as  
well as save you some bandwidth.

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [October 11, 2008, 10:16am UTC](https://www.ruby-forum.com/t/observing-few-fields-in-a-form-best-practise/150671/3 "2008-10-11T10:16:29Z")

</div>

On Sat, Oct 11, 2008 at 6:33 AM, Vinay [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> im looking to build a page similar to a shopping cart page. When you  
> change the ‘price’ or ‘quantity’ fields, the total is calculated and  
> displayed.  
> There are other fields in the form which have no relation to this  
> action. So i guess observe\_form is not required. Can someone tell me  
> what the best practise is for this?

In the server you certainly have code to compute the total of the  
cart. It probably works with :decimal numbers that are guaranteed to  
add and subtract as expected.

Depending on the details of the application, my first approach would  
be to just send the entire form to the server and let that code  
compute what is needed. I do that myself in a commercial online  
invocing application.

Alternatively you duplicate the server code in JavaScript (that’s  
duplication because you’ll still needed on the server). I’d only do  
that if there’s a strong reason that justifies it.

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [October 11, 2008, 10:58am UTC](https://www.ruby-forum.com/t/observing-few-fields-in-a-form-best-practise/150671/4 "2008-10-11T10:58:02Z")

</div>

thank you both for your replies!  
So if we consider an invoicing application itself, what you say is  
that i need to observe the fields rate, quantity, tax, etc (say)  
independently, and then send in the whole form when even one field is  
changed? If we take an invoice example, there are going to be fields  
like invoice number, recipient, created on etc which are hardly  
necessary for this calculation right?  
This was what led me to wonder if there was an observer that watches a  
specific set of fields and performs a particular action when any one  
of them is changed and passes all the values in these fields as  
parameters. It just seems like something Rails would/should have.  
If you think sending in the whole form is the best way to do this,  
then how can i do that? I should use one observe field per input  
field, and? i dont know the syntax to pass the whole form with the  
url.

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [October 13, 2008, 11:18am UTC](https://www.ruby-forum.com/t/observing-few-fields-in-a-form-best-practise/150671/5 "2008-10-13T11:18:41Z")

</div>

I have this in place for now.

\<%= observe\_field ‘cart\_record\_attributes\_\_qty’,:frequency =\> 0.1,:url  
=\>{:action =\> ‘calculate’, :submit =\> ‘cart\_form’},:method  
=\>:get,:with =\> “‘qty=’+ element.value” %\>

i dont see the parameters being passed in the dvlpmnt log. Im  
obviously not doing this right. Help?  
The form is for an instance variable of cart.  
\<% form\_for @cart do |f| %\>  
and Record is a child model (cart has\_many records) and hence the long  
name of the qty field(cart\_record\_attributes\_\_qty).

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [October 11, 2008, 6:58pm UTC](https://www.ruby-forum.com/t/observing-few-fields-in-a-form-best-practise/150671/6 "2008-10-11T18:58:11Z")

</div>

On Sat, Oct 11, 2008 at 10:57 AM, Vinay [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> thank you both for your replies!  
> So if we consider an invoicing application itself, what you say is  
> that i need to observe the fields rate, quantity, tax, etc (say)  
> independently, and then send in the whole form when even one field is  
> changed? If we take an invoice example, there are going to be fields  
> like invoice number, recipient, created on etc which are hardly  
> necessary for this calculation right?

A general rule of thumb of mine is: Consider as a first choice the  
approach that is both easy and trivially robust.

In this case, submitting the form is just a matter of setting :submit  
=\> ‘invoice-form’ in the observers.

One could select the strictly needed fields and avoid sending the  
invoice number, customer ID, etc. to save a few bytes. But that  
introduces more complexity and a coupling point between the form and  
JavaScript.

So the question I ask myself is: is there some measure that justifies  
that? Is the application having issues with the load or something due  
to that? The answer so far is no, so I just :submit the form.

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [October 13, 2008, 11:36am UTC](https://www.ruby-forum.com/t/observing-few-fields-in-a-form-best-practise/150671/7 "2008-10-13T11:36:55Z")

</div>

I vaguely remember coming across a post which mentioned how to pass  
multiple form element values through the :with option. Does anyone  
know how to do that?

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [October 13, 2008, 12:57pm UTC](https://www.ruby-forum.com/t/observing-few-fields-in-a-form-best-practise/150671/8 "2008-10-13T12:57:21Z")

</div>

🙂 thanks for that. Its working now.

On Oct 13, 2:45 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![vinay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/6a8cbe/32.png) [@vinay](https://www.ruby-forum.com/u/vinay)\
**Post date:** [October 13, 2008, 11:46am UTC](https://www.ruby-forum.com/t/observing-few-fields-in-a-form-best-practise/150671/9 "2008-10-13T11:46:55Z")

</div>

On Oct 13, 10:36 am, Vinay [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I vaguely remember coming across a post which mentioned how to pass  
> multiple form element values through the :with option. Does anyone  
> know how to do that?

That may have been my blog post here:

> **[:with or :without you: link\_to\_remote's mysterious parameter - Space Vatican](https://www.spacevatican.org/2008/5/17/with-or-without-you-link_to_remote-s-mysterious-parameter/)**
>
> One of the nice things about Rails is that it makes it really easy to get off the ground with Ajax, thanks to the link\_to\_remote helper (and all the …

Fred
