Here's a Ruby on Rails index page with Turbo Frame that includes a sortable table:
Controller (app/controllers/users_controller.rb)
class UsersController < ApplicationController
def index
@users = User.all
@users = @users.order(sort_column => sort_direction) if sort_column.present?
@users = @users.page(params[:page]).per(10) # Using Kaminari for pagination
respond_to do |format|
format.html
format.turbo_stream
end
end
private
def sort_column
%w[name email created_at].include?(params[:sort]) ? params[:sort] : 'created_at'
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : 'desc'
end
end
Index View (app/views/users/index.html.erb)
<div class="container mx-auto px-4 py-8">
<div class="flex justify-between items-center mb-6">
<h1 class="text-3xl font-bold text-gray-900">Users</h1>
<%= link_to "New User", new_user_path, class: "bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded" %>
</div>
<%= turbo_frame_tag "users_table" do %>
<div class="bg-white shadow-md rounded-lg overflow-hidden">
<div class="overflow-x-auto">
<table class="min-w-full divide-y divide-gray-200">
<thead class="bg-gray-50">
<tr>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
#
</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
<%= link_to "Name", users_path(sort: "name", direction: next_direction("name")),
data: { turbo_frame: "users_table" },
class: "hover:text-gray-700 #{sort_indicator("name")}" %>
</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
<%= link_to "Email", users_path(sort: "email", direction: next_direction("email")),
data: { turbo_frame: "users_table" },
class: "hover:text-gray-700 #{sort_indicator("email")}" %>
</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
<%= link_to "Created At", users_path(sort: "created_at", direction: next_direction("created_at")),
data: { turbo_frame: "users_table" },
class: "hover:text-gray-700 #{sort_indicator("created_at")}" %>
</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">
Actions
</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
<% @users.each_with_index do |user, index| %>
<tr class="hover:bg-gray-50">
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
<%= (params[:page].to_i - 1) * 10 + index + 1 if params[:page].present? %>
<%= index + 1 unless params[:page].present? %>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<div class="text-sm font-medium text-gray-900"><%= user.name %></div>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<div class="text-sm text-gray-900"><%= user.email %></div>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
<%= user.created_at.strftime("%B %d, %Y") %>
</td>
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
<%= link_to "View", user_path(user), class: "text-indigo-600 hover:text-indigo-900 mr-3" %>
<%= link_to "Edit", edit_user_path(user), class: "text-green-600 hover:text-green-900 mr-3" %>
<%= link_to "Delete", user_path(user), method: :delete,
confirm: "Are you sure?",
class: "text-red-600 hover:text-red-900" %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
<!-- Pagination -->
<div class="bg-white px-4 py-3 flex items-center justify-between border-t border-gray-200 sm:px-6">
<div class="flex-1 flex justify-between sm:hidden">
<%= paginate @users, theme: 'twitter-bootstrap-4',
params: { sort: params[:sort], direction: params[:direction] },
data: { turbo_frame: "users_table" } %>
</div>
<div class="hidden sm:flex-1 sm:flex sm:items-center sm:justify-between">
<div>
<p class="text-sm text-gray-700">
Showing <span class="font-medium"><%= @users.offset_value + 1 %></span>
to <span class="font-medium"><%= [@users.offset_value + @users.limit_value, @users.total_count].min %></span>
of <span class="font-medium"><%= @users.total_count %></span> results
</p>
</div>
<div>
<%= paginate @users, theme: 'twitter-bootstrap-4',
params: { sort: params[:sort], direction: params[:direction] },
data: { turbo_frame: "users_table" } %>
</div>
</div>
</div>
</div>
<% end %>
</div>
Helper Methods (app/helpers/users_helper.rb)
module UsersHelper
def sort_indicator(column)
if params[:sort] == column
params[:direction] == "asc" ? "sort-asc" : "sort-desc"
else
""
end
end
def next_direction(column)
if params[:sort] == column && params[:direction] == "asc"
"desc"
else
"asc"
end
end
end
CSS for Sort Indicators (app/assets/stylesheets/application.css)
.sort-asc::after {
content: " ↑";
color: #3B82F6;
}
.sort-desc::after {
content: " ↓";
color: #3B82F6;
}
/* Additional table styling */
.table-hover tbody tr:hover {
background-color: #F9FAFB;
}
Routes (config/routes.rb)
Rails.application.routes.draw do
resources :users
# other routes...
end
Key Features:
-
Turbo Frame: The entire table is wrapped in a turbo_frame_tag "users_table" which allows for seamless updates without full page reloads
-
Sortable Columns: Click on Name, Email, or Created At headers to sort. The data: { turbo_frame: "users_table" } ensures the sorting happens within the frame
-
Visual Sort Indicators: Up/down arrows show current sort direction
-
Row Indexing: Each row shows its position number, accounting for pagination
-
Pagination: Uses Kaminari gem with Turbo Frame support
-
Responsive Design: Uses Tailwind CSS classes for a clean, responsive layout
To use this code, make sure you have:
- Turbo Rails gem installed
- Kaminari gem for pagination (
gem 'kaminari')
- A User model with name, email, and created_at fields
The table will update smoothly when sorting or paginating without full page refreshes thanks to Turbo Frames.