will_paginate在Rails 3.1中使用jQuery加载更多按钮

问题描述:

我读过其他人已经关注了Endless Page railscast并将其修改为使用加载更多按钮。但是,当我尝试修改它时,我最终会追加相同的作品。换句话说,它仍然从当前页面拉出。will_paginate在Rails 3.1中使用jQuery加载更多按钮

当我点击我的“加载更多”分区,我上传触发此:

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

在我的控制,我有

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

我想知道如果我应该能够以某种方式使用will_paginate的.next_page。

有没有人使用Rails 3和jQuery做了类似will_paginate的事情?

尝试是这样的

在产品查看

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

和JS响应

$("#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 %> 
+0

谢谢!我错误地试图通过渲染的params而不是原始的link_to。 – Oakland510

+0

谢谢!更好的解决方案(当js被禁用时): 除非@ products.next_page.nil? = link_to'show_more',method_that_returns_products_path(:sort => sort_column,:direction => sort_direction,:page => @ products.next_page),:remote => true,:id =>'show_more_link' – Gady

您是否在请求期间通过了:page参数?例如:http://yourdomain.com/products?page=3

如果不是,will_paginate将不知道您想要偏移查询,并且它只会返回结果的第1页。