# Handling forms with arrays and their errors

**URL:** <https://www.ruby-forum.com/t/handling-forms-with-arrays-and-their-errors/115436>\
**Category:** Rails\
**Created:** [September 25, 2007, 9:17am UTC](https://www.ruby-forum.com/t/handling-forms-with-arrays-and-their-errors/115436 "2007-09-25T09:17:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gga](https://www.ruby-forum.com/u/gga)\
**Post date:** [September 25, 2007, 9:17am UTC](https://www.ruby-forum.com/t/handling-forms-with-arrays-and-their-errors/115436/1 "2007-09-25T09:17:27Z")

</div>

Hi, I’m building my first rails application and all has gone well so  
far.

However, one area where I’m running into some trouble as I have not  
seen any explanation for is creating forms for models where the form  
inputs do not match the rails’ model. Specifically, dealing with  
arrays.

To make myself clear. Take your typical shopping cart, where the cart  
list the items.  
Following most rail tutorials, an order is mapped to a class that has  
many line items.

I want to provide the ability to have the user type in the quantity  
for any line item in the cart and then update all of its values (in a  
simple way, no AJAX).

However, as there’s many line\_items, form\_for cannot be used.  
Instead, I have to rely on creating the form using form\_tag. And  
here’s the issue. I have to name each field in the form by, say,  
concatenating the index to the form’s name. Like  
“line\_item\_quantity\_1”, “line\_item\_quantity\_1”, etc.

Then my controller has to do the reverse mapping.

And, in the case of a wrong input (say quantity \<= 0), the field  
should also be hilited in rails default way.

Now, this seems to be a lot of repetitive work, so I was hoping there  
was an easier way to do this. Also, I admit I still do not quite  
understand how to properly handle errors() on models when this  
happens. For example, if during the checking of the quantity I do a  
line\_item.error( “message” ), I don’t see the error displayed by  
flash, but I do see it if i were to do order.error( field,  
“message” ).

Can anyone provide some clarification?
