Ruby on Rails Sunday, October 26, 2014

On Oct 26, 2014, at 10:36 AM, Dave Aronson <googlegroups.2.TRex@codosaur.us> wrote:

> On Sat, Oct 25, 2014 at 8:07 PM, <mike.rood@comcast.net> wrote:
>
>> 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

>
> -Dave
>
> --
> Dave Aronson, freelance software developer (details @ www.Codosaur.us);
> see also www.PullRequestRoulette.com, Blog.Codosaur.us, www.Dare2XL.com
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAHxKQii5omycWRM69%2Bq8_a13_xCPT7Kr2mHbMpnHvOzaDa1zNw%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/D944617C-E8A1-4D47-8A13-9C7569F228CD%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment