Skip to content

Instantly share code, notes, and snippets.

@cassioscabral
Forked from zolzaya/kaminari_load_more.md
Created October 29, 2013 17:15
Show Gist options
  • Save cassioscabral/7218810 to your computer and use it in GitHub Desktop.
Save cassioscabral/7218810 to your computer and use it in GitHub Desktop.

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