How to pass search parameters to pagination links


#1

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


#2

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?


#3

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


#4

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.


#5

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


#6

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…


#7

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}) %>

#8

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}) %>


#9

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}) %>