Persistent form data (using sessions?)

Hi all,

I’ve been struggling with this problem for the past 2 days and still
haven’t found a good solution - any help would be appreciated!

I have a ‘list’ page that lists all products from a DB. On top of that
listing I have a little box where the user can specify ‘filter’
parameters (eg, book name equals ‘foo’). The list below is refreshed
via ajax everytime one of these filter parameters changes.

What I am trying to do is make the filter parameters persistent
throughout the session, ie if the user set a filter (name, type of book,
etc) in the filter box on the list page, then leaves the list view to do
something else (eg look at the details of one of the books), and then
returns, I’d like a) the list on the list page to still be filtered and,
more importantly, I’d like b) the various text_fields that make up the
filter box to be populated with the values the user selected earlier.

I can do a) with a Filter class that I added to the book model which
generates a SQL conditions string according to the user selections. I’m
struggling with b) - I can’t find an elegant way to preserve the
text_field states between sessions. I’ve tried putting the filter box
into a new controller and model, but then I run into problems because
the filter model doesn’t have any database backend.

Thanks for any suggestions you might have!!

Felix

Anyone?

you need to use sessions.

Just do something like

@session[:filterform] = Hash.new
@session[:filterform] = @params[:book].attributes

@book = Book.new
@book.attributes = @session[:filterform] if @session[:filterform]

then in your view all the <%= text_field ‘book’, ‘isbn’ %> should be
populated

adam

Adam D. wrote:

you need to use sessions.

Thanks a lot for your reply! It seems so much more obvious now! Here’s
what I did (pretty much exactly as you suggested, I just had to modify
it a bit):

# store the filter parameters
@site_search = Site.new(params[:site])
@session[:filterform] = Hash.new
@session[:filterform] = @site_search.attributes

…and then in the list action
@site = Site.new
@site.attributes = @session[:filterform] if @session[:filterform]

Thanks again!

Felix