Using this thread as an example i've managed to get endless scrolling to work but not very well. Scrolling isn't smooth and displays the newly-loaded posts more than once. Here is all my code:
Posts controller:
@posts = Post.all.paginate(:page => params[:page], :per_page => 5)
respond_to do |format|
format.html
format.js
end
posts.js.coffee:
$ ->
$('#pins').imagesLoaded ->
$('#pins').masonry
itemSelector: '.box'
isResizable: true
if $('.pagination').length
$(window).scroll ->
url = $('.pagination .next_page a').attr('href')
if url && $(window).scrollTop() > $(document).height() - $(window).height() - 50
# What to do at the bottom of the page
$('.pagination').text("Loading more posts...")
$.getScript(url)
$(window).scroll()
posts/index.js.erb:
$boxes = $('<%= j render(@posts) %>')
$('#pins').append( $boxes ).imagesLoaded( function(){
$('#pins').masonry( 'reload');
});
<% if @posts.next_page %>
$('.pagination').replaceWith('<%= j will_paginate(@posts) %>');
<% else %>
$('.pagination').remove();
<% end %>
I then wrapped the view with this:
<div class="post-box pins-masonry" data-no-turbolink>