We gonna use Pagy for pagination
bundle add pagy
include defaults on application_controller.rb
include Pagy::Backend
And on application_helper.rb
include Pagy::Frontend
Now Include pagination to your controller
def index
@pagy, @todos = pagy(Current.user.todos)
end
On the view include 2 new turbo frame tags one with current page rendering records And another one including next page
<%= turbo_frame_tag :page, @pagy.page do %>
<%= render @todos %>
<% if @pagy.next %>
<%= turbo_frame_tag :page, @pagy.next, src: todos_path(page: @pagy.next), loading: :lazy do %>
<%= spinner.html_safe %> Loading...
<% end %>
<% end %>
<% end %>