I'm trying to implement infinite scroll using will_paginate. Given my current code, the output is that on each page, content is printed twice (...) and when I get to the bottom of the page, only regular pagination is there (no infinite scroll). When I get to the last page, the pagination is gone (so pagination.remove is working).
The app is simple. Users are listed, so other users can give them recommendations.
index.html.erb:
<div id="users">
<%= render :partial => 'help/user' %>
</div>
<%= will_paginate @users %>
<script>
$('#users').append('<%= j render :partial =>'help/user' %>');
<% if @users.next_page %>
$('.pagination').replaceWith('<%= j will_paginate(@users) %>');
<% else %>
$('.pagination').remove();
<% end %>
<% sleep 1 %>
</script>
I placed the script above there, because when I put it on index.js.erb, it didn't do anything.
help.coffee (the above file is in help folder, and managed by help controller):
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...')
$.getScript(url)
$(window).scroll()
help controller:
@users = User.all.order("updated_at DESC").paginate(page: params[:page], per_page: 5)
respond_to do |format|
format.html
format.js
end