Forum: Ruby on Rails How do I bind a form to filter a table?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
7db3b1680807d393d40260591b4ae5e0?d=identicon&s=25 sa 125 (sa125)
on 2008-12-25 15:19
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

<div id="filters">
  <%= select :product,
             Vendor.find(:all, :order => 'name').collect {|v| [,] },
             :prompt => 'select vendor' %>
  <%= text_field_tag 'query', params['query'] %>

<table id="ptable">
  <%= render :partial => 'product', :collection => @products %>

<% observe_field 'query', :update => 'ptable', :url => { :action =>
'filter' },
                           :with => 'query' }

# -------------------
# in app/views/products/_product.html.erb
  <td><%=h %></td>
  <td><%=h %></td>
     <% if product.stocked? %>
     <% else %>
     <% end %>

# -------------------
# in app/controllers/product_controller.rb

def filter
  cond = ["name LIKE ?", "%#{params[:query]%"] unless
  @products = Product.find(:all, :conditions => cond)
  render :partial => 'product', :collection => @products if

(this is based on a great tutorial found at

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.
This topic is locked and can not be replied to.