# How to pass search parameters to pagination links

**URL:** <https://www.ruby-forum.com/t/how-to-pass-search-parameters-to-pagination-links/98183>\
**Category:** Rails\
**Created:** [January 28, 2006, 6:37am UTC](https://www.ruby-forum.com/t/how-to-pass-search-parameters-to-pagination-links/98183 "2006-01-28T06:37:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jeet](https://www.ruby-forum.com/u/jeet)\
**Post date:** [January 28, 2006, 6:37am UTC](https://www.ruby-forum.com/t/how-to-pass-search-parameters-to-pagination-links/98183/1 "2006-01-28T06:37:35Z")

</div>

I have a search page with a text\_field as below

\<%= text\_field ‘program’, ‘program\_name’ %\>

After I perform the search in my controller I go to a search results  
view which shows search results and uses Rails pagination. Now how to  
pass the search paramters (i.e. params[:program][:program\_name] in my  
controller) to the pagination links (next and previous) in my search  
results view so that the last search parameters are passed on to the  
search method in the controller that builds the next page.

–Jeet

---

<div class="post-metadata">

**Author:** ![jeet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jeet](https://www.ruby-forum.com/u/jeet)\
**Post date:** [January 28, 2006, 7:29am UTC](https://www.ruby-forum.com/t/how-to-pass-search-parameters-to-pagination-links/98183/2 "2006-01-28T07:29:52Z")

</div>

hmm, on a side note, if we didn’t want to keep on researching the db and  
rather cache the results in lets say the session variable, i suppose we  
would have to assign it an id and such, but ya, how do u pass variables  
onto  
the pagination link?

---

<div class="post-metadata">

**Author:** ![jeet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jeet](https://www.ruby-forum.com/u/jeet)\
**Post date:** [January 28, 2006, 3:44pm UTC](https://www.ruby-forum.com/t/how-to-pass-search-parameters-to-pagination-links/98183/3 "2006-01-28T15:44:33Z")

</div>

Easiest way would be to save the search term in your session and use  
when  
you construct the pages. Another way would be to generate your own page  
links and add on the search parameter to each on.

-Nick

---

<div class="post-metadata">

**Author:** ![jeet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jeet](https://www.ruby-forum.com/u/jeet)\
**Post date:** [January 28, 2006, 11:57pm UTC](https://www.ruby-forum.com/t/how-to-pass-search-parameters-to-pagination-links/98183/4 "2006-01-28T23:57:23Z")

</div>

The issue is not the storage but how to send a hash to a link\_to?

zbyte pepsi-hola wrote:

> Thing about storing it in your session though, it would be messed up if  
> someone did more then one search.

---

<div class="post-metadata">

**Author:** ![jeet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jeet](https://www.ruby-forum.com/u/jeet)\
**Post date:** [January 28, 2006, 11:21pm UTC](https://www.ruby-forum.com/t/how-to-pass-search-parameters-to-pagination-links/98183/5 "2006-01-28T23:21:51Z")

</div>

Thing about storing it in your session though, it would be messed up if  
someone did more then one search.

---

<div class="post-metadata">

**Author:** ![jeet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jeet](https://www.ruby-forum.com/u/jeet)\
**Post date:** [January 29, 2006, 7:45am UTC](https://www.ruby-forum.com/t/how-to-pass-search-parameters-to-pagination-links/98183/6 "2006-01-29T07:45:13Z")

</div>

or rather, pagination. I think it should be a feature in pagination (if  
it  
doesn’t already exist). After all, I shouldn’t rewrite some code that  
already exists…

---

<div class="post-metadata">

**Author:** ![jeet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jeet](https://www.ruby-forum.com/u/jeet)\
**Post date:** [January 29, 2006, 10:30pm UTC](https://www.ruby-forum.com/t/how-to-pass-search-parameters-to-pagination-links/98183/7 "2006-01-29T22:30:20Z")

</div>

Navjeet C. wrote:

> I have a search page with a text\_field as below
> 
> \<%= text\_field ‘program’, ‘program\_name’ %\>

If I understand the problem correctly, I can give an example of how I’ve  
been doing this sort of thing, but I’m not positive it’s correct:

I have a params[:type] parameter that is passed into a search, so the  
controller method in the account controller looks like:

```
def list_by_type
      @type = params[:type]
      @user_pages_by_type, @users_by_type = paginate :users, :conditions 
=> ["U_Type = ?", @type], :per_page => 20
  end
```

And it could be called in an rhtml file like:

```
<%= link_to "Retail Accounts",
  :controller => "account",
  :action => "list_by_type",
  :type => "R" %>
```

In the list\_by\_type.rhtml file:

```
<%= link_to 'Previous page', { :page => 
@user_pages_by_type.current.previous, :type => @type } if 
@user_pages_by_type.current.previous %>
<%= link_to 'Next page', { :page => @user_pages_by_type.current.next, 
:type => @type } if @user_pages_by_type.current.next %>
| Pages : <%= pagination_links(@user_pages_by_type, :params => {:type => @type}) %>
```

---

<div class="post-metadata">

**Author:** ![jeet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jeet](https://www.ruby-forum.com/u/jeet)\
**Post date:** [January 29, 2006, 10:31pm UTC](https://www.ruby-forum.com/t/how-to-pass-search-parameters-to-pagination-links/98183/8 "2006-01-29T22:31:43Z")

</div>

Navjeet C. wrote:

> I have a search page with a text\_field as below
> 
> \<%= text\_field ‘program’, ‘program\_name’ %\>

If I understand the problem correctly, I can give an example of how I’ve  
been doing this sort of thing, but I’m not positive it’s correct:

I have a params[:type] parameter that is passed into a search, so the  
controller method in the account controller looks like:

def list\_by\_type  
@type = params[:type]  
@user\_pages\_by\_type, @users\_by\_type = paginate :users, :conditions  
=\> [“U\_Type = ?”, @type], :per\_page =\> 20  
end

And it could be called in an rhtml file like:

\<%= link\_to “Retail Accounts”,  
:controller =\> “account”,  
:action =\> “list\_by\_type”,  
:type =\> “R” %\>

In the list\_by\_type.rhtml file:

\<%= link\_to ‘Previous page’, { :page =\>  
@user\_pages\_by\_type.current.previous,  
:type =\> @type } if @user\_pages\_by\_type.current.previous %\>  
\<%= link\_to ‘Next page’, { :page =\> @user\_pages\_by\_type.current.next,  
:type =\> @type } if @user\_pages\_by\_type.current.next %\>  
| Pages : \<%= pagination\_links(@user\_pages\_by\_type, :params =\> {:type =\> @type}) %\>

---

<div class="post-metadata">

**Author:** ![jeet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jeet](https://www.ruby-forum.com/u/jeet)\
**Post date:** [January 30, 2006, 5:23pm UTC](https://www.ruby-forum.com/t/how-to-pass-search-parameters-to-pagination-links/98183/9 "2006-01-30T17:23:32Z")

</div>

Leah,

What if the params :type is not a simple string but a hash e.g.  
program[:name] as generated by ActionView helper text\_field ‘program’,  
‘name’

–Navjeet

Leah C. wrote:

> Navjeet C. wrote:
> 
> > I have a search page with a text\_field as below
> > 
> > \<%= text\_field ‘program’, ‘program\_name’ %\>
> 
> If I understand the problem correctly, I can give an example of how I’ve  
> been doing this sort of thing, but I’m not positive it’s correct:
> 
> I have a params[:type] parameter that is passed into a search, so the  
> controller method in the account controller looks like:
> 
> def list\_by\_type  
> @type = params[:type]  
> @user\_pages\_by\_type, @users\_by\_type = paginate :users, :conditions  
> =\> [“U\_Type = ?”, @type], :per\_page =\> 20  
> end
> 
> And it could be called in an rhtml file like:
> 
> \<%= link\_to “Retail Accounts”,  
> :controller =\> “account”,  
> :action =\> “list\_by\_type”,  
> :type =\> “R” %\>
> 
> In the list\_by\_type.rhtml file:
> 
> \<%= link\_to ‘Previous page’, { :page =\>  
> @user\_pages\_by\_type.current.previous,  
> :type =\> @type } if @user\_pages\_by\_type.current.previous %\>  
> \<%= link\_to ‘Next page’, { :page =\> @user\_pages\_by\_type.current.next,  
> :type =\> @type } if @user\_pages\_by\_type.current.next %\>  
> | Pages : \<%= pagination\_links(@user\_pages\_by\_type, :params =\> {:type =\> @type}) %\>
