Skip to content

Instantly share code, notes, and snippets.

@akbarb24
Forked from zolzaya/kaminari_load_more.md
Created October 23, 2019 02:17
Show Gist options
  • Save akbarb24/25eec7eb7bb80bd57db88f99d8c685e4 to your computer and use it in GitHub Desktop.
Save akbarb24/25eec7eb7bb80bd57db88f99d8c685e4 to your computer and use it in GitHub Desktop.
Most easy "load more" behavior implementation for Kaminari.

In your view:

<%= link_to "Load more", posts_path(@posts, page: @posts.current_page+1), id: "load-more-posts", remote: true %>

In your controller:

respond_to :html, :js, only: [:index]

def index
  @posts = Post.recent.page(params[:page]).per(10)
  respond_with @posts
end

In your index.js.erb

$("#posts-container").append("<%= j(render(partial: "posts", collection: @posts)) %>");

<% if @posts.last_page? %>
  $("#load-more-posts").hide();
<% else %>
  # hide loader indicator
  $("#load-more-posts").attr("href", "<%= posts_path(@posts, page: (@posts.current_page+1)) %>");
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment