Forum: Ruby on Rails how to pass search parameters to pagination links

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Navjeet C. (Guest)
on 2006-01-28 07:37
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
zbyte pepsi-hola (Guest)
on 2006-01-28 08:29
(Received via mailing list)
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?
Nick S. (Guest)
on 2006-01-28 16:44
(Received via mailing list)
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
zbyte pepsi-hola (Guest)
on 2006-01-29 00:21
(Received via mailing list)
Thing about storing it in your session though, it would be messed up if
someone did more then one search.
Navjeet C. (Guest)
on 2006-01-29 00:57
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.
zbyte pepsi-hola (Guest)
on 2006-01-29 08:45
(Received via mailing list)
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....
Leah C. (Guest)
on 2006-01-29 23:30
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:

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

And it could be called in an rhtml file like:

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

In the list_by_type.rhtml file:

<pre>
<%= 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}) %>
</pre>
Leah C. (Guest)
on 2006-01-29 23:31
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}) %>
Navjeet C. (Guest)
on 2006-01-30 18:23
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}) %>
This topic is locked and can not be replied to.