# Search Forms with Pagination

**URL:** <https://www.ruby-forum.com/t/search-forms-with-pagination/72354>\
**Category:** Rails\
**Created:** [February 23, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/search-forms-with-pagination/72354 "2006-02-23T16:32:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jamie\_van\_Dyke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/977dab/32.png) [@Jamie\_van\_Dyke](https://www.ruby-forum.com/u/Jamie_van_Dyke)\
**Post date:** [February 23, 2006, 4:32pm UTC](https://www.ruby-forum.com/t/search-forms-with-pagination/72354/1 "2006-02-23T16:32:25Z")

</div>

Hi there everyone, I’ve recently moved to RoR and am still struggling  
with the basics of what used to be second nature in php, so on with  
my question:

I have a database with products, and I need to create a search form  
that paginates the results. What I have so far in my controller is:

def list  
# If the values are nil, then set them, otherwise wrap them in %  
# Also, set up our view instance variable, to re-populate the  
search form  
@view = Machine.new

```
@view.make = getParam("make")
@view.model = getParam("model")

# Search the database and populate our paging object
@filtered_machines = Machine.find(:all, :conditions => ["make

```

LIKE ? AND model LIKE ? AND for\_sale = 1 AND web\_width != 0",  
sqlWildcard(@view.make),  
sqlWildcard(@view.model)])  
@machine\_pages, @machines = paginate\_collection(@filtered\_machines)  
end

def getParam(name, default="")  
params.fetch(“view”, {}).fetch(name, default)  
end

def sqlWildcard(value)  
value.blank? ? ‘%’ : “%#{value}%”  
end

Now, the list page is a full listing, and it has a filter box at the  
top that people can type in and hit Filter and it searches ad returns  
a cut down version. But what I have in my view paginates the  
results, but the links to other pages drop the search. I’ve probably  
not done this the right way, I’m open to suggestions on best  
practises etc. Here’s my view:

\<%= render :partial =\> ‘search’ %\>  
  
\<%= link\_to ‘Previous page’, { :page =\>  
@machine\_pages.current.previous } if @machine\_pages.current.previous %\>  
\<%= link\_to ‘Next page’, { :page =\>  
@machine\_pages.current.next, :make =\> @view.make, :model =\>  
@view.model } if @machine\_pages.current.next %\>  
  
\<%= render :partial =\> “machine”, :collection =\> @machines %\>

Everything works apart from the pagination. But again, this feels a  
little messy, so please feel free to rip apart the way I’ve done it.

Thanks,  
Jamie van Dyke  
Fear of Fish

---

<div class="post-metadata">

**Author:** ![Jamie\_van\_Dyke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/977dab/32.png) [@Jamie\_van\_Dyke](https://www.ruby-forum.com/u/Jamie_van_Dyke)\
**Post date:** [February 23, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/search-forms-with-pagination/72354/2 "2006-02-23T17:15:04Z")

</div>

My problem is not that paging isn’t working, sorry, but that the  
links aren’t working. If I click “Next Page” it reverts back to the  
entire database rather than the filtered results. My links are done  
like so:

\<%= link\_to ‘Previous page’, { :page =\>  
@machine\_pages.current.previous } if @machine\_pages.current.previous %\>  
\<%= link\_to ‘Next page’, { :page =\>  
@machine\_pages.current.next, :make =\> @view.make, :model =\>  
@view.model } if @machine\_pages.current.next %\>

Thanks,  
Jamie van Dyke  
Fear of Fish

---

<div class="post-metadata">

**Author:** ![Jamie\_van\_Dyke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/977dab/32.png) [@Jamie\_van\_Dyke](https://www.ruby-forum.com/u/Jamie_van_Dyke)\
**Post date:** [February 23, 2006, 5:51pm UTC](https://www.ruby-forum.com/t/search-forms-with-pagination/72354/3 "2006-02-23T17:51:35Z")

</div>

On Thu, 2006-02-23 at 16:12 +0000, Jamie van Dyke wrote:

> 

* * *

then you have to either store the ‘filters’ that you used in a session  
variable and use that to do the paging or pass the params[:variable]  
with each link\_to

Craig

---

<div class="post-metadata">

**Author:** ![Jamie\_van\_Dyke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/977dab/32.png) [@Jamie\_van\_Dyke](https://www.ruby-forum.com/u/Jamie_van_Dyke)\
**Post date:** [February 23, 2006, 4:44pm UTC](https://www.ruby-forum.com/t/search-forms-with-pagination/72354/4 "2006-02-23T16:44:39Z")

</div>

On Thu, 2006-02-23 at 15:31 +0000, Jamie van Dyke wrote:

> search form  
> @machine\_pages, @machines = paginate\_collection(@filtered\_machines)  
> Now, the list page is a full listing, and it has a filter box at the  
> @machine\_pages.current.next, :make =\> @view.make, :model =\>  
> @view.model } if @machine\_pages.current.next %\>  
>   
> \<%= render :partial =\> “machine”, :collection =\> @machines %\>
> 
> Everything works apart from the pagination. But again, this feels a  
> little messy, so please feel free to rip apart the way I’ve done it.

* * *

use the methodology just like a scaffold would create…

@filtered\_machines\_pages = paginate(:  
:filtered\_machines,  
:conditions =\> [“make LIKE ? AND model LIKE ? AND for\_sale = 1 AND  
web\_width != 0”, sqlWildcard(@view.make), sqlWildcard(@view.model)])

Something like that should work

Craig

---

<div class="post-metadata">

**Author:** ![Jamie\_van\_Dyke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/977dab/32.png) [@Jamie\_van\_Dyke](https://www.ruby-forum.com/u/Jamie_van_Dyke)\
**Post date:** [February 23, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/search-forms-with-pagination/72354/5 "2006-02-23T18:58:19Z")

</div>

> My problem is not that paging isn’t working, sorry, but that the  
> links aren’t working. If I click “Next Page” it reverts back to the  
> entire database rather than the filtered results. My links are done  
> like so:
> 
> \<%= link\_to ‘Previous page’, { :page =\>  
> @machine\_pages.current.previous } if @machine\_pages.current.previous %\>  
> \<%= link\_to ‘Next page’, { :page =\>  
> @machine\_pages.current.next, :make =\> @view.make, :model =\>  
> @view.model } if @machine\_pages.current.next %\>

I have used:

pagination\_links @FOO\_pages, { :params =\> {‘filter’ =\>  
@params[:filter]} }

to append my current filter string as a GET parameter on the page links.

---

<div class="post-metadata">

**Author:** ![Jamie\_van\_Dyke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/977dab/32.png) [@Jamie\_van\_Dyke](https://www.ruby-forum.com/u/Jamie_van_Dyke)\
**Post date:** [February 23, 2006, 6:12pm UTC](https://www.ruby-forum.com/t/search-forms-with-pagination/72354/6 "2006-02-23T18:12:46Z")

</div>

That’s what I was hoping to avoid. Thanks for your help, mate.

Thanks,  
Jamie van Dyke  
Fear of Fish
