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.
74a56286718bc003d6c7438f7efa1e3a?d=identicon&s=25 Navjeet Chabbewal (jeet)
on 2006-01-28 06: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
B91e98954bc09fc208a098c8f10e4fd5?d=identicon&s=25 zbyte pepsi-hola (Guest)
on 2006-01-28 07: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?
Cb610750ee94ca103aef4b2dc7b1b768?d=identicon&s=25 Nick Stuart (Guest)
on 2006-01-28 15: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
B91e98954bc09fc208a098c8f10e4fd5?d=identicon&s=25 zbyte pepsi-hola (Guest)
on 2006-01-28 23: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.
74a56286718bc003d6c7438f7efa1e3a?d=identicon&s=25 Navjeet Chabbewal (jeet)
on 2006-01-28 23: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.
B91e98954bc09fc208a098c8f10e4fd5?d=identicon&s=25 zbyte pepsi-hola (Guest)
on 2006-01-29 07: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....
3accfb92a7a47de5a84e71da69d57210?d=identicon&s=25 Leah Cunningham (heinous)
on 2006-01-29 22:30
Navjeet Chabbewal 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>
3accfb92a7a47de5a84e71da69d57210?d=identicon&s=25 Leah Cunningham (heinous)
on 2006-01-29 22:31
Navjeet Chabbewal 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}) %>
74a56286718bc003d6c7438f7efa1e3a?d=identicon&s=25 Navjeet Chabbewal (jeet)
on 2006-01-30 17: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 Cunningham wrote:
> Navjeet Chabbewal 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.