简体   繁体   中英

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?

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 %>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM