In your view:
<%= link_to "Load more", posts_path(@posts, page: @posts.current_page+1), id: "load-more-posts", remote: true %>
In your controller:
respond_to :html, :js, only: [:index]
def index
@posts = Post.recent.page(params[:page]).per(10)
respond_with @posts
end
In your index.js.erb
$("#posts-container").append("<%= j(render(partial: "posts", collection: @posts)) %>");
<% if @posts.last_page? %>
$("#load-more-posts").hide();
<% else %>
# hide loader indicator
$("#load-more-posts").attr("href", "<%= posts_path(@posts, page: (@posts.current_page+1)) %>");
<% end %>
Hi this was working for me ...
$('#load-more-posts').replaceWith("<%= escape_javascript(link_to_next_page(@products, 'Load more', :remote => true, :id=>'load-more-posts' ))%>");
Any idea how to add ?page=XX to url? Code bellow is working but only for the first click... second click does not add ?page=XX to url....
$(document).on('turbolinks:load', function() {
$('#load_more_link').on('click', function() {
url = $(this).attr('href');
window.history.pushState(url, window.title, url);
});
});