Ransack Table

Needle has *no* chance in this haystack
Includes a Ruby component and corresponding Rails helper
Ruby
This is a paid-only premium component
Premium

Built to work seamlessly with the Ransack gem for advanced searching and sorting functionality. Extends the base Table component with built-in search form integration and sortable headers.

Example

Title Studio ▼ Platform Year Actions
Project Cars 3 Slightly Mad Studios Multi 2020 View
OutRun Sega AM2 Arcade 1986 View
Automobilista Reiza Studios PC 2016 View
Rocket League Psyonix Multi 2015 View
Wipeout Psygnosis PlayStation 1995 View
Gran Turismo 7 Polyphony Digital PlayStation 2022 View
Forza Horizon 5 Playground Games Xbox/PC 2021 View
Mario Kart 8 Nintendo EPD Nintendo 2014 View
F-Zero Nintendo EAD Nintendo 1990 View
Crash Team Racing Naughty Dog PlayStation 1999 View
<%= nk_search_form_for @q do |f| %>
  <%= f.search_field :name_cont, placeholder: "Search..." %>
  <%= f.submit "Search" %>
<% end %>

<%= nk_ransack_table(@q) do |t| %>
  <%= t.thead do %>
    <%= t.tr do %>
      <%= t.sortable_th :name, "Name" %>
      <%= t.sortable_th :email, "Email" %>
      <%= t.sortable_th :created_at, "Created" %>
      <%= t.th "Actions" %>
    <% end %>
  <% end %>
  <%= t.tbody do %>
    <% @users.each do |user| %>
      <%= t.tr do %>
        <%= t.td { user.name } %>
        <%= t.td { user.email } %>
        <%= t.td { l(user.created_at, format: :short) } %>
        <%= t.td { nk_button "Edit", size: :xs } %>
      <% end %>
    <% end %>
  <% end %>
<% end %>

Install

Premium

Set up Nitro Kit then

bin/rails app:template \
  LOCATION="https://nitrokit.dev/t/premium/ransack_table?license=LICENSE-KEY"

Dependencies

This component adds third-party dependencies:

Usage

nk_search_form_for(record, **options) { |f| ... }
PropertyDefaultDescription
recordRansack search object The Ransack search object, typically created with Model.ransack(params[:q]).
**attrsHTML attributes for <form> element
nk_ransack_table(record, **attrs) { |t| ... }
PropertyDefaultDescription
recordRansack search object The same Ransack search object used with the search form.
**attrsHTML attributes for <table> element
table.sortable_th(attribute, label = nil, **attrs) { ... }
PropertyDefaultDescription
attributeSymbol The model attribute to sort by (e.g., :name, :created_at).
labelString Display text for the header. Auto-generated from attribute if not provided.
default_order:asc or :desc Default sort direction when this header is first clicked.
hide_indicatorBoolean Whether to hide the sort direction indicator arrows.
**attrsHTML attributes for <th> element

Note: Since Ransack Table is a subclass of the base Table component, it accepts all the same APIs and methods. Refer to the Table documentation for additional styling options and methods.

Controller Setup

class UsersController < ApplicationController
  def index
    @q = User.ransack(params[:q])
    @users = @q.result.includes(:profile)
    @pagy, @users = pagy(@users)
  end
end