Ruby on Rails - AJAX pagination with Kaminari
I needed a pagination feature, without the need of a full page reload. So I had a look at Kaminari, which is the only paging gem that seems to be active. All though, the default implementation of Kaminari with AJAX enabled by just doing:
1
= paginate @users, :remote => true
Didn’t seem to work at all…
Here’s my implementation which is fully working. I’m using news articles as an example.
news_controller.rb
1
2
3
4
5
6
7
8
def index
@news = News.all.page(params[:page]).per(10)
respond_to do |format|
format.html
format.js { render :layout => false }
end
end
_ news.haml
1
2
3
4
5
6
7
8
%table.table.table-condensed.table-striped.table-hover
%tr
%th Title
%th Content
- @news.each do |n|
%tr
%td= n.title
%td= n.content
index.haml
1
2
3
4
5
#news-table
= render 'news'
#news-paginator
= paginate @news, :remote => true
index.js.erb
1
2
$('#news-table').html('<%= escape_javascript(raw(render 'news')) %>');
$('#news-paginator').html('<%= escape_javascript(paginate(@news, remote: true)) %>');
Hope this implementation works for those that faced the same problem as I, with the default AJAX pagination not working.
Feel free to PM me if any questions.
Regards