Updating multiple records with form

I have an app that has two models – products and details. I’m building
a form that allows a user to create a new product and specify a
description, etc.

The form needs to allow for creation of multiple items in the details
model/table. for example, I have a block of form items that collect
info for the men’s version of the product – inventory count for each
size, etc. And a similar block of form items for women’s versions.

I’m wondering how I can specify that each inventory count box creates a
new detail record with the right values (ie,
product_id=123456,gender=mens, size=XL, inventory=20)

Geoff D. wrote:

new detail record with the right values (ie,
product_id=123456,gender=mens, size=XL, inventory=20)

All form helpers have an :index option that will separate
the posted parameters of each item by a separate hash id.
Note: Be careful to put the index option for select helper
calls in the html_options hash rather than the options hash.


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

Mark,

Thanks for your suggestion. I’m playing around w/ :index, but still
having difficulty. I’ve tried about every syntax I can imagine to get
the correct info into :index, without luck. This is the closest I’ve
gotten.

product name
<%= text_field 'product', 'name' %>

product description
<%= text_field 'product', 'description' %>

price
<%= text_field 'product', 'price' %>

Men's

brand
<%= text_field 'detail', 'brand', :index=>"gender=>Mens" %>

fabric blend
<%= text_field 'detail', 'blend', :index=>"gender=>Mens" %>

Mark Reginald J. wrote:

Geoff D. wrote:

new detail record with the right values (ie,
product_id=123456,gender=mens, size=XL, inventory=20)

All form helpers have an :index option that will separate
the posted parameters of each item by a separate hash id.
Note: Be careful to put the index option for select helper
calls in the html_options hash rather than the options hash.


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

Okay, this is getting me quite close to where I need to be. Thanks much

I’m going to have to parse this stuff in the model or controller, right?
No way Ruby will automatically put subindexed items into the correct
table?

Geoff D. wrote:

Men's

brand
<%= text_field 'detail', 'brand', :index=>"gender=>Mens" %>

fabric blend
<%= text_field 'detail', 'blend', :index=>"gender=>Mens" %>

<%= text_field :details, :brand, :index => ‘mens’ %>
is the sort of thing you want, but since you really need two
levels of indexing you may want to use text_field_tag instead:

<% for gender in %w(mens, womens) %>
<% for size in %w(S M L XL) %>
<%= text_field_tag “inventory[#{gender}][#{size}]” %>
<% end
end %>

Which will give you a inventory hash nested by gender and
size.

If you also want the text boxes pre-filled with existing
or previously-posted values, you’ll have to add a parameter
to the text helpers to pick up the right value from either
the details records or params hash.


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