#
Troubleshooting
Don't Paginate Unordered PostgreSQL Collections!
@pagy, @records = pagy(unordered)
# behind the scenes, pagy selects the page of records with:
unordered.offset(pagy.offset).limit(pagy.limit)
From the PostgreSQL Documentation
When using LIMIT, it is important to use an ORDER BY clause that constrains the result rows into a unique order. Otherwise you will get an unpredictable subset of the query's rows.
Ensure the PostgreSQL collection is ordered!
# results will be predictable with #order
ordered = unordered.order(:id)
@pagy, @records = pagy(ordered)
Don't rely on ARIA default with multiple nav elements!
Pagy sets the aria-label
attribute of its nav
elements with the translated and pluralized pagy.aria_label.nav
that finds in
the locale files. That would be (always) "Page"/"Pages"
for the en
locale.
Since the nav
or role="navigation"
elements of a HTML document are considered landmark roles
, they
should be uniquely aria-identified in the page.
Pass your own aria_label
to each nav!
<%# Explicitly set the aria_label string %>
<%== pagy_nav(@pagy, aria_label: 'Search result pages') %>
Don't duplicate attributes with the :anchor_string
!
<%== pagy_bootstrap_nav(@pagy, anchor_string: 'class="my-class"', **vars) %>
The class
attribute with a value of "pagination"
is already added by the pagy_bootstrap_nav
so it's a duplicate HTML
attribute which is invalid html.
Easily check the native component attributes!
pagy demo
# or: bundle exec pagy demo
# ...and point your browser at http://0.0.0.0:8000
In the specific bootstrap
example you could override the default bootstrap "pagination"
class by adding other classes with:
@pagy, @records = pagy_bootstrap_nav(collection, classes: 'pagination my-class')
There has been a single report of a slow last page using very big DB tables. It's a pure DB problem and it's not caused by pagy or by any other ruby code (#696, #704), but a simple pagy override may avoid it:
## override pagy_get_items
def pagy_get_items(collection, pagy)
limit = pagy.last == pagy.page ? pagy.in : pagy.limit
collection.offset(pagy.offset).limit(limit)
end