# Filtering records using a \<select\> control

**URL:** <https://www.ruby-forum.com/t/filtering-records-using-a-select-control/152206>\
**Category:** Rails\
**Created:** [October 31, 2008, 9:24am UTC](https://www.ruby-forum.com/t/filtering-records-using-a-select-control/152206 "2008-10-31T09:24:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhernandez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@jhernandez](https://www.ruby-forum.com/u/jhernandez)\
**Post date:** [October 31, 2008, 9:24am UTC](https://www.ruby-forum.com/t/filtering-records-using-a-select-control/152206/1 "2008-10-31T09:24:35Z")

</div>

I have recently been working on a simple list action and view that  
will allow a user to filter the objects that are selected by changing  
a category value in a control.

In this case, the resources being listed are restaurants. The view  
also contains a list of all restaurant categories, which defaults to a  
selection of “All Categories”, that will allow them to filter the  
restaurants displayed by category.

I have a working implementation, however, it feels like a bit of a  
hack. In the view, I am using the form\_for and collection\_select  
helpers, because they handle auto-selecting the appropriate value and  
don’t require me to assemble an options hash. The part that feels like  
a hack to me is that in order to get the auto-selection to work, I am  
creating a Category model object in the controller, and assigning the  
id value that was submitted through the form as the object’s “id”  
property. Also, I have to explicitly set the “name” property in the  
htmlOptions hash, so that the field gets submitted as  
params[:category\_id] rather than params[:category\_id].

I am new to Rails, and I wanted to see if anyone felt there was a  
cleaner way for me to implement this functionality. Anyhow, here’s the  
code. Any suggestions would be greatly appreciated:

#\*\* Controller \*\*#  
def index  
(@category = Category.new()).id = params[:category\_id]

```
if (!@category.id.nil?)
  @restaurants = Restaurant.find_all_by_category_id(@category.id)
else
  @restaurants = Restaurant.find(:all)
end

respond_to do |format|
  format.html { @categories = Category.find(:all) } #

```

index.html.erb  
format.xml { render :xml =\> @restaurants }  
end  
end

#\*\* View (index.rhtml) \*\*#  
\<% form\_for( :category, :html =\> {:method =\> :get}) do |f| %\>  
\<%=  
f.collection\_select(  
:id,  
@categories,  
:id,  
:name,  
{:include\_blank =\> ‘All Categories’},  
{:onchange =\> ‘this.form.submit();’, :name =\>  
‘category\_id’}  
)  
%\>  
\<% end %\>

Lastly, I was wondering if it would it make more sense to move the  
category selection form to a partial, rather than embed it directly in  
index.rhtml with these rest of the code (which is not shown above)?

Thanks for the help,

Justin H.

---

<div class="post-metadata">

**Author:** ![jhernandez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e68b1a/32.png) [@jhernandez](https://www.ruby-forum.com/u/jhernandez)\
**Post date:** [October 31, 2008, 10:43pm UTC](https://www.ruby-forum.com/t/filtering-records-using-a-select-control/152206/2 "2008-10-31T22:43:32Z")

</div>

Justin H. wrote:

> hack. In the view, I am using the form\_for and collection\_select  
> cleaner way for me to implement this functionality. Anyhow, here’s the  
> end  
> \<%=  
> \<% end %\>  
> \<% form\_tag do %\>  
> \<%= select\_tag :category\_id,  
> options\_from\_collection\_for\_select(  
> @categories,  
> :id,  
> :name,  
> params[:category\_id])  
> %\>  
> \<% end %\>

> Lastly, I was wondering if it would it make more sense to move the  
> category selection form to a partial, rather than embed it directly in  
> index.rhtml with these rest of the code (which is not shown above)?

Only if you reuse it elsewhere.

–  
Rails Wheels - Find Plugins, List & Sell Plugins -  
[http://railswheels.com](http://railswheels.com)
