Insert multiple new child records with the parent

Hello,

I am implementing a classic invoice application. On the create new
invoice page
I want to allow users to add new line items using AJAX:


Invoice Date [ ]
Client [ ]

Line Items

  1. [ ] $[ ]

When the user clicks on the link I would like to add
a new
empty line item below the first using AJAX, without saving anything to
the
database. A line item consists of a description and a price.

I want the line items, on save, to be passed to the controller as an
array,
e.g., [{‘description’ => ‘line a’, ‘cost’ => ‘100’}, {‘description’ =>
‘line
b’, ‘cost’ => ‘200’}]. I can’t find the correct naming convention for
the
textboxes to produce this data structure in the controller.

I have tried:
<%= text_field_tag(“line_items[0][description]”) %>
<%= text_field_tag(“line_items[0][cost]”) %>
<%= text_field_tag(“line_items[1][description]”) %>
<%= text_field_tag(“line_items[1][cost]”) %>

which results in a hash of hashes:
“invoice_default”=>{“0”=>{“cost”=>“100”, “description”=>“line a”},
“1”=>{“cost”=>“200”, “description”=>“line b”}}

The problem with the hash of hashes is that I need to give each new line
item
a key. Because the line items don’t yet exist in the database I have to
give
them an artificial key, a sequential number. It is then tricky to find
the last
number in the sequence in order to add a new empty line item via AJAX.

I have also tried:
<%= text_field_tag(“line_items[][description]”) %>
<%= text_field_tag(“line_items[][cost]”) %>
<%= text_field_tag(“line_items[][description]”) %>
<%= text_field_tag(“line_items[][cost]”) %>

This results in a hash of hashes with a zero length string as the key,
e.g,
{""=>{“cost”=>“100”, “description”=>“line a”}} and only the first line
item
is passed to the controller.

Does anyone have any ideas on how to solve this the rails way, i.e.,
elegantly?
I’m sure I am missing something terribly obvious.

Any help would be appreciated. I’ll write up a wiki entry on this
problem if
anyone has a solution.

I apologise if this has already been discussed on the list. Both the
gmane and
forum bridge searches are down.

Thanks,
Mark

Mark wrote:

Line Items
database. A line item consists of a description and a price.
<%= text_field_tag(“line_items[0][description]”) %>
a key. Because the line items don’t yet exist in the database I have to

This results in a hash of hashes with a zero length string as the key,
e.g,
{""=>{“cost”=>“100”, “description”=>“line a”}} and only the first line
item
is passed to the controller.

Does anyone have any ideas on how to solve this the rails way, i.e.,
elegantly?

You can’t get params in the single array form you want it, but you
can get a pair of description and cost arrays using

<%= text_field_tag(“line_items[description][]”) %>
<%= text_field_tag(“line_items[cost][]”) %>

which you can then collate into single object attribute sets.


We develop, watch us RoR, in numbers too big to ignore.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs