All Engineering

Anatomy of an Index Page

The parts of a page that lists records, and what each one does.

Most apps have the same page somewhere: a list of records you can search, filter, sort, select and page through. Admin panels have it, dashboards have it, and so does every CRUD screen. Because it's so common, it tends to be built in a hurry, one part at a time, until the page has a search box nobody trusts and a filter that resets when you hit the back button.

This post takes that page apart. The diagram below is a skeleton: gray bars where text would be and real labels only where the label is the point. Each numbered part has a section after it that covers what the part is for, what tends to go wrong, and how I'd build it in Rails.

/courses
An index page for courses, drawn as a skeleton. The numbers match the sections below; the letters mark the column types in part 6.

The record

An index page needs something to list, so the examples use one model: a Course on a learning platform. It has a status, an instructor, some counts and some money, which is enough to fill every part of the page without inventing filler.

Text
Course      title, slug, status, category, price_cents,
            instructor_id, lessons_count, enrollments_count,
            completion_rate, published_at, updated_at
Instructor  name, avatar
Lesson      course_id, position, title, kind, duration, status

Lessons don't show up on this page. They belong to a course, so they get their own list on the course's show page, which is the next post.

1. Page header

The header answers two questions: what am I looking at, and what's the main thing I can do here? That means a title, a total count and one primary button. "New course" is the primary action on almost every index page, so it gets the filled button and nothing else up here competes with it.

The count is a quiet sign that the page is working. When someone applies a filter and the count drops from 312 to 9, they know the filter applied before they read a single row.

2. Metrics

A short strip of numbers that describe the collection as a whole: active students, average completion, revenue, new enrollments. Three or four is plenty. Any more and the strip turns into a dashboard and pushes the table, which is what people came for, below the fold.

Decide whether the numbers describe everything or the current filter, and label them so it's obvious. Mixing the two is the most common bug here: the table shows Mara's courses and the revenue tile still shows everyone's.

Ruby
@metrics = {
  students:    Enrollment.active.count,
  completion:  Course.published.average(:completion_rate),
  revenue:     Enrollment.where(created_at: 30.days.ago..).sum(:price_cents),
  enrollments: Enrollment.where(created_at: 30.days.ago..).count
}

These are aggregate queries over the whole table, so they're a good fit for Rails.cache.fetch with a short expiry once the table gets big.

3. View tabs

Tabs are saved filters for the slices people check most. All, Published, Drafts and Archived cover how most people think about courses, and the counts on each tab tell you where the work is before you click.

In Rails, the tabs map straight onto an enum, and each tab is a link with a different status param:

Ruby
class Course < ApplicationRecord
  enum :status, { draft: 0, published: 1, archived: 2 }
end

# Counts for every tab in one query
Course.group(:status).count
# => { "draft" => 41, "published" => 248, "archived" => 23 }

4. Search, filter and sort

The toolbar narrows the list. Search is free text over the fields people remember (title and slug). Filters are for fields with a fixed set of values (instructor, category). Sort sets the order, and its current value should always be visible, because "why is this course at the top?" is a question the page should answer without anyone having to ask.

The rule I care about most: all of this lives in the URL. If the search, filters, sort and page are query params, the view can be bookmarked and shared, and the back button works. If they live in JavaScript state, the user loses them every time they open a course and come back.

With Ransack and Turbo, a GET form updates the table in a frame and pushes the new URL into history:

ERB
<%= search_form_for @q, html: { data: { turbo_frame: "courses", turbo_action: "advance" } } do |f| %>
  <%= f.search_field :title_or_slug_cont, placeholder: "Search courses" %>
  <%= f.select :instructor_id_eq, Instructor.pluck(:name, :id), include_blank: "Any instructor" %>
  <%= f.select :category_eq, Course.distinct.pluck(:category), include_blank: "Any category" %>
<% end %>

<%= turbo_frame_tag "courses" do %>
  <%# table and pagination %>
<% end %>

Ransack 4 makes you list the searchable fields with ransackable_attributes on the model, which is a good thing: the URL is user input, and only the columns you name can be searched.

5. Bulk actions

When rows are selected, a bar appears with the actions that make sense for many records at once: publish, archive, delete. It replaces the toolbar or sits right above the table, and it always says how many rows are selected, so nobody archives forty courses thinking it was two.

Two details are easy to miss. Destructive actions get a different color and a confirmation step. And if "select all" is offered, it should say whether it means the 25 rows on this page or all 312 that match the filter, since those are very different actions.

The checkboxes can live outside the form by pointing at it with the HTML form attribute, which keeps the table markup simple:

ERB
<%= form_with url: bulk_courses_path, method: :patch, id: "bulk" do |f| %>
  <%= f.button "Archive", name: "bulk_action", value: "archive" %>
<% end %>

<%# in each row %>
<%= check_box_tag "course_ids[]", course.id, false, form: "bulk" %>

6. The table

The table is the page. Every column type has its own rules, and most of them come down to two things: make the row easy to scan, and make the right thing clickable.

6a. Selection

A checkbox per row, plus one in the header to select every row on the page. The header checkbox has three states: none, some (indeterminate) and all. It drives the bulk actions bar from part 5.

6b. Primary identifier

The one column that names the record. It's the widest, it comes first after the checkbox, and it's the link to the show page. A second line underneath (slug, ID or short description) helps when two titles look alike. Make the title the link, not the whole row: a clickable row fights with the checkboxes, the menus and text selection.

6c. Status

A colored label from a small, fixed set. Color carries the meaning at a glance, so keep it consistent across the app: green is live, amber needs attention, gray is out of the way. The text still has to say the status, because color alone doesn't work for everyone.

6d. Relationships

The instructor is another record, shown as an avatar and a name. This is where N+1 queries come from, so load the association with the list:

Ruby
@q.result.includes(:instructor)

6e. Numbers

Right-aligned with tabular figures, so digits line up and you can compare values by length. Show a dash rather than a 0 when the number doesn't apply yet (a draft has no students, which isn't the same as having zero). Counts like lessons_count come from a counter cache, so rendering 25 rows doesn't run 25 count queries:

Ruby
class Lesson < ApplicationRecord
  belongs_to :course, counter_cache: true
end

6f. Dates

Relative time ("2d ago") is easier to scan; the exact timestamp goes in a tooltip for when it matters. Pick the date people actually sort by. For courses that's usually updated_at, not created_at.

ERB
<time datetime="<%= course.updated_at.iso8601 %>" title="<%= l course.updated_at, format: :long %>">
  <%= time_ago_in_words(course.updated_at) %> ago
</time>

6g. Row actions

A ⋯ menu at the end of the row for actions on one record: edit, duplicate, archive. It keeps the row quiet until someone needs it. Anything people do constantly can earn a visible button instead, but one is usually the limit before the row gets noisy.

7. Pagination

Pagination says where you are ("1–25 of 312"), lets you move, and optionally lets you choose the page size. The range text matters more than the page buttons: it confirms the filter worked and tells you how much is left.

Pagy handles the query and the helpers. Because the page number is a query param like everything else, it survives a trip to the show page and back:

Ruby
@pagy, @courses = pagy(@q.result.includes(:instructor), limit: 25)

Changing a filter should reset the page to 1. Otherwise someone on page 9 filters down to 12 results and gets an empty page.

8. Empty states

The diagram shows the page full, but it spends a lot of its life in one of three other states, and each one needs different words:

Brand new, filtered to nothing, and loading.
  • Nothing exists yet. This is onboarding. Explain what a course is and put the "New course" button in the middle of the page.
  • Nothing matches. The records exist but the filter hides them. Say which filters are on and offer a "Clear filters" button. Never show the onboarding message here; it tells people their data is gone.
  • Loading. Skeleton rows the same height as real rows, so the page doesn't jump when the data arrives. With Turbo Frames, the old rows can simply stay visible and dim until the new ones replace them.

The URL is the state

If there's one idea to take from all this, it's that parts 3, 4 and 7 are the same feature. Tabs, search, filters, sort and page are all query params on one GET request, and the controller turns them into one scoped query:

Text
/courses?q[status_eq]=1&q[instructor_id_eq]=4&q[s]=updated_at+desc&page=2

Build it that way and you get bookmarking, sharing, the back button and server rendering without extra work. It also sets up the next page: clicking a course title opens its show page, and its "back to courses" link can return to this exact view.

The next post, Anatomy of a Show Page, opens one of these courses and takes that page apart the same way, including the lessons table inside it, which is a small index page of its own.