How to Implement ajax pagination with will_paginate gem

前端 未结 6 955
遥遥无期
遥遥无期 2020-12-02 10:19

I am using will_paginate gem in my ROR project to show the records in pages.

I want the next page to be loaded without reloading the whole

6条回答
  •  忘掉有多难
    2020-12-02 10:42

    You could just use JQuery:

    Controller:

    def index
      @posts = Post.paginate(:page => params[:page])      
    
      respond_to do |format|
        format.html
        format.js
      end
    end
    

    Then in index.html.erb:

    <%= j render 'post_content' %>

    In partial '_post_content.html.erb':

    <%= will_paginate @posts %>
    <% @posts.each do |post| %>
      

    <%= post.content %>

    <% end %>

    pagination.js:

    $(document).ready(function() {
      $('.pagination > a').attr('data-remote', 'true');
    });
    

    index.js.erb:

    $('#post-content').html("<%= j render 'post_content' %>")
    

    Make sure to add the

    $('.pagination > a').attr('data-remote', 'true');
    

    again in your JS template (index.js.erb), so it sets the links data-remote again when the Ajax runs.

提交回复
热议问题