# Accesing post array

**URL:** <https://www.ruby-forum.com/t/accesing-post-array/175499>\
**Category:** Rails\
**Created:** [September 14, 2009, 8:33pm UTC](https://www.ruby-forum.com/t/accesing-post-array/175499 "2009-09-14T20:33:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jask](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jask](https://www.ruby-forum.com/u/jask)\
**Post date:** [September 14, 2009, 8:33pm UTC](https://www.ruby-forum.com/t/accesing-post-array/175499/1 "2009-09-14T20:33:25Z")

</div>

I’ve been following the “Agile Web D. 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:

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

\<% @items.each do |item| %\>

\<%=h item.product.name%\> \<%= text\_field\_tag item, item.quantity, { :size =\> 2, :maxlength =\> 3 } %\>

 \<% 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:** ![jask](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jask](https://www.ruby-forum.com/u/jask)\
**Post date:** [September 14, 2009, 9:21pm UTC](https://www.ruby-forum.com/t/accesing-post-array/175499/2 "2009-09-14T21:21:25Z")

</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:** ![jask](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jask](https://www.ruby-forum.com/u/jask)\
**Post date:** [September 15, 2009, 8:26pm UTC](https://www.ruby-forum.com/t/accesing-post-array/175499/3 "2009-09-15T20:26:26Z")

</div>

On Sep 14, 4:12 pm, “David A. Black” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## from something slightly different (my Item objects have a description {“items”=\>{“345698069”=\>{“description”=\>“third item”},
> 
> David A. Black, Director  
> Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))  
> Ruby/Rails training, consulting, mentoring, code review  
> Book: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://www.manning.com/black2))

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

---

<div class="post-metadata">

**Author:** ![jask](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jask](https://www.ruby-forum.com/u/jask)\
**Post date:** [September 14, 2009, 11:14pm UTC](https://www.ruby-forum.com/t/accesing-post-array/175499/4 "2009-09-14T23:14:24Z")

</div>

Hi –

On Mon, 14 Sep 2009, Pardee, Roy wrote:

> 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:

\<% @items.each do |item| %\>  
\<% fields\_for “items[]”, item do |ff| %\>

Description: \<%= ff.text\_field :description %\>

  
\<% end %\>  
\<% end %\>

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

{“items”=\>{“345698069”=\>{“description”=\>“third item”},  
“345698067”=\>{“description”=\>“first item”},  
“345698068”=\>{“description”=\>“second item”}},  
“commit”=\>“Buy”,  
etc. }

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

David

–  
David A. Black, Director  
Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))  
Ruby/Rails training, consulting, mentoring, code review  
Book: The Well-Grounded Rubyist ([The Well-Grounded Rubyist](http://www.manning.com/black2))
