Skip to content

Instantly share code, notes, and snippets.

@wenweih
Forked from adamrobbie/Gemfile
Created December 29, 2016 16:50
Show Gist options
  • Save wenweih/62c5e35e6c979b5588a756ce7c43d7d2 to your computer and use it in GitHub Desktop.
Save wenweih/62c5e35e6c979b5588a756ce7c43d7d2 to your computer and use it in GitHub Desktop.
Rails Endless pagination
jQuery ->
if $('.pagination').length
$(window).scroll ->
url = $('.pagination .next_page').attr('href')
if url && $(window).scrollTop() > $(document).height() - $(window).height() - 50
$('.pagination').text("Fetching more examples...")
$.getScript(url)
$(window).scroll()
def index
@exampless = Examples.order("our_column").page(params[:page]).per_page(10)
end
gem 'will_paginate'
<div id="examples">
<%= render @examples %>
</div>
<%= will_paginate @examples %>
// Our ajax triggered code that corresponds with the rails controller action method.
$('#example').append('<%= j render(@exampless) %>');
<% if @exampless.next_page %>
$('.pagination').replaceWith('<%= j will_paginate(@examples) %>');
<% else %>
$('.pagination').remove();
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment