Paginating results of a form post?

Hi,

My case is slightly more complex than below but I’ll give a simple
example to
describe the problem.

There is a ‘book’ table. A user can search for books by typing keywords
for book title and author name in a form. For example: show all books
that include the word ‘peace’ in the title. The search results needs to
be paginated.

controller will look roughly like this

def search_results

title = params[:search][:title]
authorname = params[:search][:authorname]

form the sql query

sql = 'select * from books… where title =… and author_id=… "

create paginator and results. I’ll be using a custom pagination here

since it’s a find_by_sql.

I know how to do this, the problem is not here…

@book_pages, @books = …

end

now, the view includes first page of results, the pagination links with
page number in the url and all… the problem is that when clicking on
them (for example clicking on ‘next page’) will never work since it
doesn’t include the request parameter of the book anymore. There is no
longer params[:search][:title] and params[:search][:authorname]. so
clicking on ‘/book/search_results/page=2’ will fail saying that
params[:search][:title] is NIL…

any idea how can i acheive this?

Thanks!

On 9/25/06, Alon G. [email protected] wrote:

be paginated.

doesn’t include the request parameter of the book anymore. There is no
longer params[:search][:title] and params[:search][:authorname]. so
clicking on ‘/book/search_results/page=2’ will fail saying that
params[:search][:title] is NIL…

any idea how can i acheive this?

The most common ways of doing it are hidden form variables or the
session. The Scaffolding Extensions plugin [1] uses hidden form
variables to paginate search results, if you want an example of how to
do it.

[1]
http://wiki.rubyonrails.com/rails/pages/Scaffolding+Extensions+Plugin

Jeremy E. wrote:

The most common ways of doing it are hidden form variables or the
session. The Scaffolding Extensions plugin [1] uses hidden form
variables to paginate search results, if you want an example of how to
do it.

[1]
Peak Obsession

Thanks Jeremy! I completely forgot about the session as a way to store
state…
I’ll read about the scaffolding extensions to see if it’s a better way
to do it.