Rails Notes: Ajaxify Pagination

Posted by Lawrence Sun on 2014-02-22

After applying pagination in my app, I would like to ajaxify kaminari paginator. I googled and stackoverflowed this issue, and found the solution, which is quite similar to what I did in likeable function.

First, add remote: true and class id to index.html.erb.

1
2
3
4
5
6
<div id="posts">
<%= render 'shared/posts', object: @posts %>
</div>
<div id="paginator">
<%= paginate @posts, remote: true %>
</div>

Second, create a new file index.js.erb.

1
2
$("#posts").html("<%= escape_javascript render 'shared/posts', object: @posts %>");
$("#paginator").html("<%= escape_javascript(paginate(@posts, remote: true).to_s) %>");

Done.