# Filter a list

**URL:** <https://www.ruby-forum.com/t/filter-a-list/69308>\
**Category:** Rails\
**Created:** [February 16, 2006, 6:26pm UTC](https://www.ruby-forum.com/t/filter-a-list/69308 "2006-02-16T18:26:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Santiago\_Hirschfeld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@Santiago\_Hirschfeld](https://www.ruby-forum.com/u/Santiago_Hirschfeld)\
**Post date:** [February 16, 2006, 6:26pm UTC](https://www.ruby-forum.com/t/filter-a-list/69308/1 "2006-02-16T18:26:36Z")

</div>

I’d like to add some filter choices to display a list of items. These  
items has some boolean fields for example, and would like to have a  
checkbox at the top of the window to let me select how to filter the  
list.

here’s what i put in my controller

if @params[:filter].nil?  
@params[:filter] = { ‘sent’ =\> “1”}  
end

… generate the corresponding @items array …

here’s what i did in my view

\<%= form\_tag :action =\> ‘list\_items’ %\>  
\<%= check\_box ‘filter’,‘sent’ %\>  
\<%= submit\_tag ‘Filter’ %\>  
\<%= end\_form\_tag %\>

 ..... 

inspecting the @params value, it seems to has a filter[:sent] key,  
with the correct value (1 if checked) but the check button is always  
unchecked when the page is generated.

Am i so wrong doing this? is there a better way?

---

<div class="post-metadata">

**Author:** ![Santiago\_Hirschfeld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@Santiago\_Hirschfeld](https://www.ruby-forum.com/u/Santiago_Hirschfeld)\
**Post date:** [February 17, 2006, 5:10am UTC](https://www.ruby-forum.com/t/filter-a-list/69308/2 "2006-02-17T05:10:37Z")

</div>

anyone? please

---

<div class="post-metadata">

**Author:** ![Santiago\_Hirschfeld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@Santiago\_Hirschfeld](https://www.ruby-forum.com/u/Santiago_Hirschfeld)\
**Post date:** [February 17, 2006, 7:25am UTC](https://www.ruby-forum.com/t/filter-a-list/69308/3 "2006-02-17T07:25:06Z")

</div>

Look at Enumerable.grep

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![Santiago\_Hirschfeld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@Santiago\_Hirschfeld](https://www.ruby-forum.com/u/Santiago_Hirschfeld)\
**Post date:** [February 17, 2006, 3:28pm UTC](https://www.ruby-forum.com/t/filter-a-list/69308/4 "2006-02-17T15:28:28Z")

</div>

Thanks, I know i wasn’t clear enough, i’m not a native english  
speaker. But my problem is not how to filter the list, my problem is  
with the checkbox being always unchecked and not mantaining the state  
of selected, don’t know if what i’m doing is right.  
Thanks anyway.

Can someone give me a hint? please?

2006/2/17, Tom M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Santiago\_Hirschfeld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@Santiago\_Hirschfeld](https://www.ruby-forum.com/u/Santiago_Hirschfeld)\
**Post date:** [February 18, 2006, 6:19am UTC](https://www.ruby-forum.com/t/filter-a-list/69308/5 "2006-02-18T06:19:16Z")

</div>

thanks, that did it

2006/2/17, Ammon C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Santiago\_Hirschfeld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ba0ec/32.png) [@Santiago\_Hirschfeld](https://www.ruby-forum.com/u/Santiago_Hirschfeld)\
**Post date:** [February 18, 2006, 2:17am UTC](https://www.ruby-forum.com/t/filter-a-list/69308/6 "2006-02-18T02:17:29Z")

</div>

Save the state of the checkbox when submitted in the session and then  
use the value of the session variable in the tag’s definition to  
maintain its value.
