在使用paginate中使用ajax
#代码演示
#index.htmle.erb
<div id="contacts">
<%= render @contacts %>
</div>
<div id="paginator">
<%= paginate @contacts, remote: true %>
</div>
#index.js.erb
$('#contacts').html('<%= escape_javascript render(@contacts) %>');
$('#paginator').html('<%= escape_javascript paginate(@contacts, remote: true) %>');
在上面的代码中,一旦点击paginate之后,url中就不再显示第几页,下面的代码是更新url以及保证可以前进和后退
#注意,需要安装gem jquery-rails以及进行相关配置
#application.js
(document).on('click', '.pagination a[data-remote=true]', function() {
history.pushState({}, '', $(this).attr('href'));
});
$(window).on('popstate', function() {
$.get(document.location.href);
});