# How do I bind a form to filter a table?

**URL:** <https://www.ruby-forum.com/t/how-do-i-bind-a-form-to-filter-a-table/156360>\
**Category:** Rails\
**Created:** [December 25, 2008, 3:19pm UTC](https://www.ruby-forum.com/t/how-do-i-bind-a-form-to-filter-a-table/156360 "2008-12-25T15:19:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [December 25, 2008, 3:19pm UTC](https://www.ruby-forum.com/t/how-do-i-bind-a-form-to-filter-a-table/156360/1 "2008-12-25T15:19:46Z")

</div>

Hi - I’m trying to enable dynamic filtering of a table in my app, using  
selection lists and a search box. Here’s the basic layout of the table:

# -------------------

# in app/views/products/index.html.erb

 \<%= select :product, :vendor\_id, Vendor.find(:all, :order =\> 'name').collect {|v| [v.name, v.id] }, :prompt =\> 'select vendor' %\> \<%= text\_field\_tag 'query', params['query'] %\>
 \<%= render :partial =\> 'product', :collection =\> @products %\>

| Product | Vendor | Stocked? |
| --- | --- | --- |

\<% observe\_field ‘query’, :update =\> ‘ptable’, :url =\> { :action =\>  
‘filter’ },  
:with =\> ‘query’ }

# -------------------

# in app/views/products/\_product.html.erb
 \<%=h product.name %\> \<%=h product.vendor.name %\> \<% if product.stocked? %\> YES \<% else %\> NO \<% end %\> 
# -------------------

# in app/controllers/product\_controller.rb

def filter  
cond = [“name LIKE ?”, “%#{params[:query]%”] unless  
params[:query].nil?  
@products = Product.find(:all, :conditions =\> cond)  
render :partial =\> ‘product’, :collection =\> @products if  
request.xml\_http\_request?  
end

(this is based on a great tutorial found at  
[http://dev.nozav.org/rails\_ajax\_table.html](http://dev.nozav.org/rails_ajax_table.html))

So far I only got the text box to respond filter the table, and even  
there my header row disappears when I do that.

So, instead of observe\_field I thought of using observe\_form and tying  
my controls in a remote\_form\_for so that I could call the ‘filter’  
action with the changes. I played with it for a while and couldn’t get  
it to work, mostly because I’m not sure how to construct my  
remote\_form\_for block with current controls (I try  
form\_remote\_for(@product) but get an :id error), and also don’t know how  
I can send all the parameters to the filter action.

Another question is more about staying DRY - how can I generalize this  
solution so that I could reuse it throughout my app with multiple tables  
(instead of adding filter action to every controller).

I would really be grateful for any help.
