# Retrieving order quantity from form

**URL:** <https://www.ruby-forum.com/t/retrieving-order-quantity-from-form/154410>\
**Category:** Rails\
**Created:** [November 28, 2008, 10:32pm UTC](https://www.ruby-forum.com/t/retrieving-order-quantity-from-form/154410 "2008-11-28T22:32:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsimone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jsimone](https://www.ruby-forum.com/u/jsimone)\
**Post date:** [November 28, 2008, 10:32pm UTC](https://www.ruby-forum.com/t/retrieving-order-quantity-from-form/154410/1 "2008-11-28T22:32:55Z")

</div>

I have a shopping cart screen where I display my available item and all  
it’s description info and then want to have the user enter a quantity.  
I show all my item fields, e.g.

\<%= @item.item\_id %\>

Then I have a field defined

\<%= text\_field ‘shopping\_cart\_item’ , ‘quantity’ %\>

In my controller, how do I access this data?

I’ve tried many different ways, I just want to get this quantity first  
to validate that it’s a number, that it’s less than the quantity  
available, etc, then create a shopping cart record.

Help, I’ve spent a long time on this.

---

<div class="post-metadata">

**Author:** ![jsimone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jsimone](https://www.ruby-forum.com/u/jsimone)\
**Post date:** [November 29, 2008, 12:52am UTC](https://www.ruby-forum.com/t/retrieving-order-quantity-from-form/154410/2 "2008-11-29T00:52:18Z")

</div>

So i changed my view to include

\<% text\_field\_tag ‘quantity’ %\>

and I changed my controller’s first line to say

quantityordered = params[:quantity]

Is this correct?

---

<div class="post-metadata">

**Author:** ![jsimone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jsimone](https://www.ruby-forum.com/u/jsimone)\
**Post date:** [November 29, 2008, 12:53am UTC](https://www.ruby-forum.com/t/retrieving-order-quantity-from-form/154410/3 "2008-11-29T00:53:50Z")

</div>

It seems to ‘work’ (doesn’t abort), but when I store it in my database I  
have a null value

---

<div class="post-metadata">

**Author:** ![jsimone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jsimone](https://www.ruby-forum.com/u/jsimone)\
**Post date:** [November 29, 2008, 12:53am UTC](https://www.ruby-forum.com/t/retrieving-order-quantity-from-form/154410/4 "2008-11-29T00:53:11Z")

</div>

On Nov 28, 2008, at 1:32 PM, Joe S. wrote:

> –  
> You probably knew this, but you need a form. So maybe:

\<% form\_for(@cart) do |f| -%\>  
\<%= f.text\_field :quantity %\>  
\<%= submit\_tag(‘ok’) %\>  
\<% end -%\>

That should get you a form. So, fire the thing up, watch your logs,  
enter something into the quantity text box and what happens? You get a  
params hash passed back to the controller. In that hash is:

:cart =\> { :quantity =\> “3” }

So in your controller, probably in the create method, just do  
something like:

quantity = params[:cart][‘quantity’]

But if all you are doing is validating, you should try to do this in  
the model anyhow.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![jsimone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@jsimone](https://www.ruby-forum.com/u/jsimone)\
**Post date:** [November 30, 2008, 9:02am UTC](https://www.ruby-forum.com/t/retrieving-order-quantity-from-form/154410/5 "2008-11-30T09:02:15Z")

</div>

Look at the Dev logs, see the params hash and use that in controller.

[Http://www.rubyplus.org](Http://www.rubyplus.org)  
Free Ruby & Rails screencasts

On Nov 28, 2008, at 3:52 PM, Joe S. \<rails-mailing-list@andreas-
