# accesing post array

**URL:** <https://discuss.rubyonrails.org/t/accesing-post-array/46055>\
**Category:** rubyonrails-talk\
**Created:** [September 14, 2009, 6:31pm UTC](https://discuss.rubyonrails.org/t/accesing-post-array/46055 "2009-09-14T18:31:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jask](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@jask](https://discuss.rubyonrails.org/u/jask)\
**Post date:** [September 14, 2009, 6:31pm UTC](https://discuss.rubyonrails.org/t/accesing-post-array/46055/1 "2009-09-14T18:31:43Z")

</div>

I've been following the "Agile Web Development with Rails" book shopping cart tutorial, and I've been trying to modify the checkout so it lets me modify the quantity of items on checkout, so far I've added a new action on the controler cart\_checkout wich has the following view:

\<h2\>Finish shopping\</h2\> \<% form\_for :cart, @cart, :url =\> { :controller =\> 'store', :action =\> 'cart\_modify'} do |f| %\>

&nbsp;&nbsp;\<% @items.each do |item| %\> \<p\> &nbsp;&nbsp;&nbsp;&nbsp;\<%=h item.product.name%\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= text\_field\_tag item, item.quantity, { :size =\> 2, :maxlength =\> 3 } %\> \</p\> &nbsp;&nbsp;\<% end %\> \<%= f.submit("Buy") %\> \<% end %\> It displays a text field with the quantity, but when I try to read the form on the controler, I don't know how to access each item and relate it to its correspondig cart item in order to modify the quantity. Can anyone point me in the right direction?.

Thanks

---

<div class="post-metadata">

**Author:** ![Pardee\_Roy](https://avatars.discourse-cdn.com/v4/letter/p/f0a364/32.png) [@Pardee\_Roy](https://discuss.rubyonrails.org/u/Pardee_Roy)\
**Post date:** [September 14, 2009, 7:04pm UTC](https://discuss.rubyonrails.org/t/accesing-post-array/46055/2 "2009-09-14T19:04:09Z")

</div>

I think you want to use the fields\_for helper so rails will give you an array of form params to hold the various items. You should also probably include a hidden text field in there to hold the id of each item so you know which quantity should go w/which item.

HTH,

-Roy

---

<div class="post-metadata">

**Author:** ![David\_A\_Black](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@David\_A\_Black](https://discuss.rubyonrails.org/u/David_A_Black)\
**Post date:** [September 14, 2009, 9:12pm UTC](https://discuss.rubyonrails.org/t/accesing-post-array/46055/3 "2009-09-14T21:12:28Z")

</div>

Hi --

> I think you want to use the fields\_for helper so rails will give you an array of form params to hold the various items. You should also probably include a hidden text field in there to hold the id of each item so you know which quantity should go w/which item.

Actually you shouldn't need a hidden field. I'm adapting this example from something slightly different (my Item objects have a description field, rather than quantity), but here's what I've got:

&nbsp;&nbsp;&nbsp;\<% @items.each do |item| %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<% fields\_for "items", item do |ff| %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<p\>Description: \<%= ff.text\_field :description %\>\</p\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<% end %\> &nbsp;&nbsp;&nbsp;\<% end %\>

and here's what's submitted to the controller in params:

{"items"=\>{"345698069"=\>{"description"=\>"third item"}, &nbsp;&nbsp;"345698067"=\>{"description"=\>"first item"}, &nbsp;&nbsp;"345698068"=\>{"description"=\>"second item"}}, &nbsp;&nbsp;"commit"=\>"Buy", &nbsp;&nbsp;&nbsp;etc. }

So now I can iterate through that hash, getting the id number and a hash of attributes each time.

David

---

<div class="post-metadata">

**Author:** ![jask](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@jask](https://discuss.rubyonrails.org/u/jask)\
**Post date:** [September 15, 2009, 6:24pm UTC](https://discuss.rubyonrails.org/t/accesing-post-array/46055/4 "2009-09-15T18:24:26Z")

</div>

I did exactly that, and it shows the array, but not the identifiers, anything else I'm missing?
