jeudi 7 mai 2020

infinite scroll in rails will_paginate not working

I'm trying to get all posts from all users to be loaded via infinite scroll using will_paginate. Right now at the end of the page it is showing Please Wait... But it is not loading.

This is home controller

class HomeController < ApplicationController
  def index
    if current_user
      @posts = Post.order(created_at: :desc).paginate(:page => params[:page], :per_page => 20)
      respond_to do |format|
        format.html
        format.js
      end
    else
      redirect_to new_user_session_path
    end
  end
end

index.html.erb

  <div class="posts">
    <% @posts.each do |post| %>
      <%= render partial: 'post', locals: { post: post } %>
    <% end %>
    <%= will_paginate @posts %>
  </div>

index.js.erb

$('#posts').append('<%= escape_javascript render(@posts) %>');
$('.pagination').replaceWith('<%= escape_javascript will_paginate(@posts) %>');

application.js

//= require rails-ujs
//= require turbolinks
//= require jquery3
//= require popper
//= require bootstrap

$(document).ready(function() {
  if ($('.pagination').length) {
    $(window).scroll(function() {
      var url = $('.pagination .next_page').attr('href');
      if (url && $(window).scrollTop() > $(document).height() - $(window).height() - 50) {
        $('.pagination').text("Please Wait...");
        return $.getScript(url);
      }
    });
    return $(window).scroll();
  }
});

I have seen this all in this tutorial: http://geekhmer.github.io/blog/2015/02/12/ruby-on-rails-with-endless-scrolling/




Aucun commentaire:

Enregistrer un commentaire