# Beginner question on paginate with params and conditions

**URL:** <https://www.ruby-forum.com/t/beginner-question-on-paginate-with-params-and-conditions/60463>\
**Category:** Rails\
**Created:** [May 14, 2006, 5:57am UTC](https://www.ruby-forum.com/t/beginner-question-on-paginate-with-params-and-conditions/60463 "2006-05-14T05:57:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![printmkr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9f8e36/32.png) [@printmkr](https://www.ruby-forum.com/u/printmkr)\
**Post date:** [May 14, 2006, 5:57am UTC](https://www.ruby-forum.com/t/beginner-question-on-paginate-with-params-and-conditions/60463/1 "2006-05-14T05:57:40Z")

</div>

I am trying to do a simple search by product name. The first page  
returns fine, but when I try to go to any other pages of the search  
results, I lose the original search parameters from my search form and  
get NilClass errors.

Here is the code in my controller:

namesearch = “%” + params[:name].strip + “%”  
@product\_pages, @products = paginate :product,  
:conditions =\> [“product\_name like :name”, {:name =\>  
namesearch}],  
:per\_page =\> 10

This seems like it should be a very common task and I assumed RoR would  
handle it automatically by adding the original search parameters to the  
pagination links. Is there a correct way to accomplish a multi-page  
search that uses conditions?

---

<div class="post-metadata">

**Author:** ![printmkr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9f8e36/32.png) [@printmkr](https://www.ruby-forum.com/u/printmkr)\
**Post date:** [May 14, 2006, 8:41pm UTC](https://www.ruby-forum.com/t/beginner-question-on-paginate-with-params-and-conditions/60463/2 "2006-05-14T20:41:30Z")

</div>

Bill C. wrote:

> I am trying to do a simple search by product name. The first page  
> returns fine, but when I try to go to any other pages of the search  
> results, I lose the original search parameters from my search form and  
> get NilClass errors.
> 
> Here is the code in my controller:
> 
> namesearch = “%” + params[:name].strip + “%”  
> @product\_pages, @products = paginate :product,  
> :conditions =\> [“product\_name like :name”, {:name =\>  
> namesearch}],  
> :per\_page =\> 10
> 
> This seems like it should be a very common task and I assumed RoR would  
> handle it automatically by adding the original search parameters to the  
> pagination links. Is there a correct way to accomplish a multi-page  
> search that uses conditions?

I’ve made a little progress on this by using merge\_params in my view.  
For the paginate links I changed the line  
pagination\_links( @@product\_pages )

to:  
pagination\_links( @product\_pages, :params =\> @params.merge (:a =\>“1”))

(I found I needed to add the dummy variable “a” in order to make it  
work)

Anyway, this gets me a step closer, but it still does not work properly.  
It adds the fields from my original search and I am able to click on  
another page after page 1 of the search result, but it end there. If I  
click on page two, “page” is added as a param, and all future pagination  
links have “page=2” added to them so every link points to page 2.

Also, even if I could get this to work correctly, I am unsure of how to  
add the “@params.merge” to the previous and next links.

I am hoping someone can point me in the right direction here. My  
experience with rails has been that if I need to work this hard on  
something so simple, I am probably missing an easier way to do it. It’s  
hard for me to imagine that the pagination stuff only works when listing  
the entire contents of a table, and can’t handle user defined searches  
from a form field.

---

<div class="post-metadata">

**Author:** ![printmkr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9f8e36/32.png) [@printmkr](https://www.ruby-forum.com/u/printmkr)\
**Post date:** [May 15, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/beginner-question-on-paginate-with-params-and-conditions/60463/3 "2006-05-15T17:10:51Z")

</div>

Hi Bill,

You are close. Your solution should look something like this:

pagination\_links( @product\_pages, :params =\> {:name =\> params[:name]})

The reason you are running into problems in the 2nd attempt is your  
@params.merge is actually overriding the :page param to hardcode it to  
the currently set :page number.

I think in your case, you only want the name to be passed around from  
page to page. The :page number will be automatically determined by  
your pagination\_links.

As an alternative, if you are passing around a lot of info across  
pages, you could store it in the session and then your page links  
don’t have to know what the data is… but I have found that in the  
majority of cases only one param is needed to be passed around.

Tom

On 5/14/06, Bill C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > :conditions => ["product_name like :name", {:name =>
> > 
> > ```
> 
> pagination\_links( @@product\_pages )  
> click on page two, “page” is added as a param, and all future pagination  
> from a form field.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Tom D.

[http://blog.atomgiant.com](http://blog.atomgiant.com)  
[http://gifthat.com](http://gifthat.com)

---

<div class="post-metadata">

**Author:** ![printmkr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9f8e36/32.png) [@printmkr](https://www.ruby-forum.com/u/printmkr)\
**Post date:** [May 15, 2006, 5:29pm UTC](https://www.ruby-forum.com/t/beginner-question-on-paginate-with-params-and-conditions/60463/4 "2006-05-15T17:29:36Z")

</div>

If you are going to implement any sort of caching I would suggest  
modifying the pagination to include the parameters as part of the url.  
If you don’t do this, page caching will not work for any pages that  
include pagination.

Charlie B.  
[http://www.recentrambles.com](http://www.recentrambles.com)
