Hi, I’m building my first rails application and all has gone well so
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
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,
Can anyone provide some clarification?