Forum: Ruby on Rails Persistent form data (using sessions?)

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.
Felix M. (Guest)
on 2006-01-30 00:51
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
Felix M. (Guest)
on 2006-01-30 21:12
Anyone?
Adam D. (Guest)
on 2006-01-30 22:46
(Received via mailing list)
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
Felix M. (Guest)
on 2006-01-31 04:09
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
This topic is locked and can not be replied to.