Graham Slick Graham Slick - 3 months ago 19
Ajax Question

Paginate posts which are displayed with Ajax

In my rails app, I am rendering posts with ajax:

<%= link_to explore_posts_path, remote: true %>


In my controller:

def explore
@posts = Post.all
respond_to do |format|
format.js
end
end


In explore.js.erb:

$('.explore-wrapper').append('\
<div class="explore-posts col-xs-9">\
<%= j render @posts %>\
</div>\
');


However, I'd like to paginate those posts with Kaminari. I managed to have Kaminari to work in my app on other pages, but posts weren't displayed with AJAX, they were displayed on page load.

I changed my explore.js.erb file:

$('.explore-wrapper').append('\
<div class="explore-posts col-xs-9">\
<%= j render @posts %>\
<%= paginate @posts %>\
</div>\
');


But now there aren't any posts displayed, so it "broke" the callback.

How can I display with AJAX paginated posts (with Kaminari) ?

Answer

The html generated by Kaminari is not escaped.

<%= j(paginate(@posts)) %>

Comments