简体   繁体   中英

Rails will_paginate and ajax append pages

I'm using the will_paginate gem with ajax to append the next page to the bottom of my index every time the user clicks "more" - the problem I'm noticing is that past the second page, clicking more just appends the second page again - what am i missing?

index.html.erb

<ul class="items" id="event-index">
<%= render 'events' %>
</ul>
<%= will_paginate @events, :previous_label => '', :next_label => 'More', :remote => true %>

index.js.erb

$('#event-index').append('<%= escape_javascript(render("events")) %>');

<% if @events.total_pages == @events.current_page %>
    $('.pagination').remove();
<% end %>
<% if @events.current_page %>
    $('.ajaxload').hide();
<% end %>

The problem with your setup is that the link that shows the next page doesn't get updated. It always shows the second page. The simplest solution I can think of is to always delete the pagination and rerender it.

$('#event-index').append('<%= escape_javascript(render("events")) %>');
$('.pagination').remove();

<% if @events.next_page %>
  $('#event-index').after('<%= will_paginate @events, :previous_label => '', :next_label => 'More', :remote => true %>');
<% end %>

<% if @events.current_page %>
  $('.ajaxload').hide();
<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM