Skip to content

Instantly share code, notes, and snippets.

@adamrobbie
Created November 13, 2012 15:28
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save adamrobbie/4066325 to your computer and use it in GitHub Desktop.
Save adamrobbie/4066325 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 %>
@sovetnik
Copy link

thanks, man :)

@Charan-Kumar
Copy link

Charan-Kumar commented Jan 3, 2017

Thank you so much :)

@leehericks
Copy link

For Rails 5 and TurboLinks 5, replace the first line of example.js.coffee as follows:

Replace jQuery -> with $(document).on "turbolinks:load", ->

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment