Beginner question on paginate with params and conditions


#1

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?


#2

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.


#3

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 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/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Tom D.

http://blog.atomgiant.com
http://gifthat.com


#4

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