Oakland510
Oakland510

Reputation: 1083

will_paginate with load more button in Rails 3.1 using jQuery

I've read that others have followed the Endless Page railscast and modified it to use a load more button. However, when I tried to modify it, I just end up appending the same productions. In other words, it's still pulling from the current_page.

When I click my "load more" div, I upload trigger this:

$("#products").append("<%= escape_javascript render( @products) %>");

In my controller, I have

@products = Product.where(:created_at => 180.days.ago..Time.now).order(sort_column + " " + sort_direction).page(params[:page]).per_page(16)

I'm wondering if I should be able to somehow use will_paginate's .next_page.

Has anyone done something similar with will_paginate using Rails 3 and jQuery?

Upvotes: 2

Views: 3254

Answers (2)

Babur Ussenakunov
Babur Ussenakunov

Reputation: 1995

Try something like this

In products view

# Show link only if there are more than one page and pass page and sorting parameters
<% unless @products.total_pages < 2 %>
    <%= link_to 'show_more', method_that_returns_products_path(:sort => sort_column, :direction => sort_direction, :page => 2), :remote => true, :id => 'show_more_link' %>
<% end %>

and in js response

$("#products").append("<%= escape_javascript render( @products) %>");

// Hide link if it is last page otherwise update link
<% if @products.total_pages == @products.current_page %>
    $("#show_more_link").hide();
<% else %>
    $("#show_more_link").attr("href", "<%= method_that_returns_products_path(:sort => sort_column, :direction => sort_direction, :page => (@products.current_page + 1)) %>");
<% end %>

Upvotes: 6

iwasrobbed
iwasrobbed

Reputation: 46703

Are you passing in the :page param during the request? For instance: http://yourdomain.com/products?page=3

If not, will_paginate won't know that you want to offset the query and it will just keep returning page 1 of the results.

Upvotes: 0

Related Questions