# Selecting line items

**URL:** <https://www.ruby-forum.com/t/selecting-line-items/175451>\
**Category:** Rails\
**Created:** [September 14, 2009, 7:13am UTC](https://www.ruby-forum.com/t/selecting-line-items/175451 "2009-09-14T07:13:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [September 14, 2009, 7:13am UTC](https://www.ruby-forum.com/t/selecting-line-items/175451/1 "2009-09-14T07:13:43Z")

</div>

I’m using check\_box but I’m not married to it, probably because I don’t  
understand it. Basically, I am presenting a list of upcoming payments  
and I want to select which items are to be paid…

\<% form\_for :supptrans, @supptrans,  
:url =\> { :action =\> “accept\_checks” } do |form| %\>  
\<% odd\_or\_even = 0  
for check\_due in @supptrans  
odd\_or\_even = 1 - odd\_or\_even  
%\>

 \<%= check\_box("check\_due", "print\_check") %\> \<%=h (check\_due.supplier.suppname) %\> \<%=h (check\_due.suppreference) %\> \<%= check\_due.duedate.to\_s.split('-')[1].to\_s + "-" + check\_due.duedate.to\_s.split('-')[2].to\_s + "-" + check\_due.duedate.to\_s.split('-')[0].to\_s %\> \<%= check\_due.ovamount.to\_fl(2) %\> \<% end %\> \<%= submit\_tag 'Print Checks' %\>\<% end %\>

and when I checked 3 different boxes, I see in a \<%= debug params %\>  
only the first line item is returned…

— !map:HashWithIndifferentAccess  
check\_due: !map:HashWithIndifferentAccess  
print\_check: “0”  
commit: Print Checks  
authenticity\_token: HqDnPuefppjsMYQhZwAq0akBrQTfnvIKrHciv81aKe0=  
action: accept\_checks  
controller: payables

and it returns a value of ‘0’ because it was unchecked.

I was hoping that it would return each line item that was checked or all  
of the line items and I could loop through them and discard the  
unchecked items but I only get the first value, nothing else.

How can I accomplish this?

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [September 14, 2009, 10:29am UTC](https://www.ruby-forum.com/t/selecting-line-items/175451/2 "2009-09-14T10:29:50Z")

</div>

On Sep 14, 6:11 am, Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m using check\_box but I’m not married to it, probably because I don’t  
> understand it. Basically, I am presenting a list of upcoming payments  
> and I want to select which items are to be paid…
> 
> \<% form\_for :supptrans, @supptrans,  
> :url =\> { :action =\> “accept\_checks” } do |form| %\>  
> \<% odd\_or\_even = 0  
> for check\_due in @supptrans  
> odd\_or\_even = 1 - odd\_or\_even  
> %\>

Take a look at the cycle helper as an alternative to this

> and it returns a value of ‘0’ because it was unchecked.
> 
> I was hoping that it would return each line item that was checked or all  
> of the line items and I could loop through them and discard the  
> unchecked items but I only get the first value, nothing else.

You should use check\_box\_tag rather than check\_box for this. Then it’s  
all about giving your check boxes the right names and the right  
values: rather than the value submitted being 1 you want the value  
submitted to be the id of the item in question. If you give the check  
boxes the same name and that name ends in [] then rails will collect  
the submitted values into an array for you

I wrote a bit more about parameter names at

> **[Parametrised to the max - Space Vatican](https://www.spacevatican.org/2008/7/18/parametrised-to-the-max/)**
>
> If you’ve played with Rails for more that about 3 minutes you’ll know that the params hash can itself contain hashes. If you use form\_for …

Fred

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [September 14, 2009, 4:55pm UTC](https://www.ruby-forum.com/t/selecting-line-items/175451/3 "2009-09-14T16:55:01Z")

</div>

On Mon, 2009-09-14 at 01:28 -0700, Frederick C. wrote:

> > for check\_due in @supptrans  
> > odd\_or\_even = 1 - odd\_or\_even  
> > %\>
> 
> Take a look at the cycle helper as an alternative to this

* * *

## I will but not for this application because there is so little of this. But thanks for mentioning it because I know I am using an antiquated method I found in one of the original AWDWR

> all about giving your check boxes the right names and the right  
> values: rather than the value submitted being 1 you want the value  
> submitted to be the id of the item in question. If you give the check  
> boxes the same name and that name ends in [] then rails will collect  
> the submitted values into an array for you
> 
> I wrote a bit more about parameter names at  
> [Parametrised to the max - Space Vatican](http://www.spacevatican.org/2008/7/18/parametrised-to-the-max)

* * *

thanks, the key was the [] and I had to re-read your message a second  
time before the light bulb turned on.

for completeness (if anyone wants what the solution was), the line  
became

```
  <td><%= check_box_tag "supptrans[]", check_due.id, false %></td>

```

Thanks Fred,

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.
