How to present search criteria before displaying list?

Hi Guys,

I have a table with several thousand records in it. They take a long time to load and are essentially useless presented altogether… I’d like present the user a search form to select a limited subset of records for display in the index function. What options are available to solve this problem and where might I look for examples or demos?

Thanks for any help you can give me.

Regards,

Mike

create a search poro (possibly extending ActiveModel for forms) with appropriate fields and perform filtering your data with it

How about, assuming you're searching for "widgets":

- Have the view expect the index controller to pass it @widgets (an
array of widget records) and @message, displaying the message and then
the records if any, and the current search in a search form (blank if
no criteria given). Let's defer for now the debates over more
advanced ideas, like encapsulating the criteria in an object, whether
that should be a PORO or not, and Sandi Metz's guideline of "only pass
one object".

- Have the index controller look at whatever criteria were presented.

- If no criteria, just set @widgets to [], and set @message to "Please
enter some search criteria."

- Else, do the search (putting results in @widgets), and then:

- If any results, set message to "The following widgets matched your
search:" (or whatever).

- Else set message to "Sorry, no widgets matched your search. Please
change your search and try again."

If they take a long time to load even with a reasonable search, you
can present a *count* of the results, and if that's over some number,
just present the count and tell the user to restrict the search some
more, like by setting @widgets empty again, and putting the count in
the message. (And/or look at *why* they take so long to load.)

-Dave

I have a table with several thousand records in it. They take a long time
to load and are essentially useless presented altogether.. I'd like present
the user a search form to select a limited subset of records for display in
the index function. What options are available to solve this problem and
where might I look for examples or demos?

How about, assuming you're searching for "widgets":

- Have the view expect the index controller to pass it @widgets (an
array of widget records) and @message, displaying the message and then
the records if any, and the current search in a search form (blank if
no criteria given). Let's defer for now the debates over more
advanced ideas, like encapsulating the criteria in an object, whether
that should be a PORO or not, and Sandi Metz's guideline of "only pass
one object".

- Have the index controller look at whatever criteria were presented.

- If no criteria, just set @widgets to [], and set @message to "Please
enter some search criteria."

- Else, do the search (putting results in @widgets), and then:

- If any results, set message to "The following widgets matched your
search:" (or whatever).

- Else set message to "Sorry, no widgets matched your search. Please
change your search and try again."

If they take a long time to load even with a reasonable search, you
can present a *count* of the results, and if that's over some number,
just present the count and tell the user to restrict the search some
more, like by setting @widgets empty again, and putting the count in
the message. (And/or look at *why* they take so long to load.)

And/or add pagination to the results. Kaminari is very popular for this, and a drop-in. Add the gem to your Gemfile, bundle install, and then in your controller simply extend the “finder” method (search or just Widget.all, if that’s what your initial view would be) with .page(params[:page]). In your view, add the pagination controls (the 1 2 3 … links) with this: <%= paginate @widgets %>. That builds a <nav> filled with links for you. By default, this will load 25 records per batch, although it is endlessly configurable.

Walter