# Progressive AJAX Filter

**URL:** <https://www.ruby-forum.com/t/progressive-ajax-filter/89472>\
**Category:** Rails\
**Created:** [March 20, 2007, 6:51pm UTC](https://www.ruby-forum.com/t/progressive-ajax-filter/89472 "2007-03-20T18:51:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![randall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@randall](https://www.ruby-forum.com/u/randall)\
**Post date:** [March 20, 2007, 6:51pm UTC](https://www.ruby-forum.com/t/progressive-ajax-filter/89472/1 "2007-03-20T18:51:18Z")

</div>

Hi Guys,

In my rails app, I present a table of data that I want to be able to  
filter using multiple criteria. I’ve created a one-line table of  
input fields above my table to accept filter criteria like this…

\<%= javascript\_include\_tag :defaults %\>  
&nbsp;

| Search:&nbsp; | \<%= text\_field\_tag("namequery", @params['namequery'], :size =\> 19) %\> | | &nbsp; | \<%= text\_field\_tag("ipquery", @params['ipquery'], :size =\> 17 ) %\> | \<--additional rows deleted here---\> |

\<%= observe\_field ‘namequery’, :frequency =\> 1,  
:update =\> ‘systemTable’,  
:before =\> “Element.show(‘spinner’)”,  
:success =\> “Element.hide(‘spinner’)”,  
:url =\> {:action =\> ‘select’},  
:with =\> ‘namequery’ %\>  
\<%= observe\_field ‘ipquery’, :frequency =\> 1,  
:update =\> ‘systemTable’,  
:before =\> “Element.show(‘spinner’)”,  
:success =\> “Element.hide(‘spinner’)”,  
:url =\> {:action =\> ‘select’},  
:with =\> ‘ipquery’ %\>

\<…additional observers deleted here\>

 \<%= render(:partial =\> "system\_table") %\>

In my controller, I have this code to process entries in those fields:

```
unless params[:namequery] == "" || params[:namequery].nil?
  conditions = "host_name LIKE \'#{@params[:namequery]}%\'"
end
unless params[:ipquery] == "" || params[:ipquery].nil?
  ipcondition = "ip_address like \'#{@params[:ipquery]}%\'"
  conditions = conditions.nil? ? ipcondition : "#{conditions} AND
                      #{ipcondition}"
end

```

I then pass use those criteria to filter the collection I send to the  
partial which displays the table of data:

```
@systems = System.find(:all, :order => sort, :conditions =>

```

conditions)

I want to be able to enter a partial system name and a partial ip  
address, e.g., and return all systems that meet both criteria.

Both filter fields work correctly alone, but if I enter one and then  
the other, only the second one entered makes it to the conditions  
clause. Using debug statements, I can see in the params[] fields only  
get updated right after the entry is detected. On the next pass  
through, the params value for the previously entered field remains “”  
even though the input is still visible in the entry field.

How do I get around this problem and detect the value that was entered  
previously?

Thanks for any advice you can give me.

– Mike

---

<div class="post-metadata">

**Author:** ![randall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@randall](https://www.ruby-forum.com/u/randall)\
**Post date:** [March 21, 2007, 2:10pm UTC](https://www.ruby-forum.com/t/progressive-ajax-filter/89472/2 "2007-03-21T14:10:23Z")

</div>

Hello,

> ```
> unless params[:namequery] == "" || params[:namequery].nil?
> 
> ```

Just while we’re here, you could shorten this to:

unless params[:namequery].blank?

> Both filter fields work correctly alone, but if I enter one and then  
> the other, only the second one entered makes it to the conditions  
> clause. Using debug statements, I can see in the params[] fields only  
> get updated right after the entry is detected. On the next pass  
> through, the params value for the previously entered field remains “”  
> even though the input is still visible in the entry field.
> 
> How do I get around this problem and detect the value that was entered  
> previously?

At the moment each observer passes along to the controller only the  
field it is observing. I would change your view to observe the whole  
form rather individual fields.

See the documentation here:

[http://api.rubyonrails.org/classes/ActionView/Helpers/](http://api.rubyonrails.org/classes/ActionView/Helpers/)  
PrototypeHelper.html#M000536

Regards,  
Andy S.

---

<div class="post-metadata">

**Author:** ![randall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b38774/32.png) [@randall](https://www.ruby-forum.com/u/randall)\
**Post date:** [March 21, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/progressive-ajax-filter/89472/3 "2007-03-21T17:40:55Z")

</div>

Hi Andy,

That’s exactly what I needed and it worked like a champ. Thank you so  
much for taking the time to help me. I appreciate your pointing out  
the blank? method too.

Regards,

Mike
