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) -- Posted via http://www.ruby-forum.com/.
Geoff Dougherty wrote:> 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)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. <p><label for="product_name">product name</label><br/> <%= text_field ''product'', ''name'' %></p> <p><label for="product_description">product description</label><br/> <%= text_field ''product'', ''description'' %></p> <p><label for="product_price">price</label><br/> <%= text_field ''product'', ''price'' %></p> <h3>Men''s</h3> <p><label for="product_brand">brand</label><br/> <%= text_field ''detail'', ''brand'', :index=>"gender=>Mens" %></p> <p><label for="sku_inventory">fabric blend</label><br/> <%= text_field ''detail'', ''blend'', :index=>"gender=>Mens" %></p> Mark Reginald James wrote:> Geoff Dougherty 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.-- Posted via http://www.ruby-forum.com/.
Geoff Dougherty wrote:> <h3>Men''s</h3> > <p><label for="product_brand">brand</label><br/> > <%= text_field ''detail'', ''brand'', :index=>"gender=>Mens" %></p> > <p><label for="sku_inventory">fabric blend</label><br/> > <%= text_field ''detail'', ''blend'', :index=>"gender=>Mens" %></p><%= 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.
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? -- Posted via http://www.ruby-forum.com/.