还是关于will_paginate的Ajax分页的老问题

rails的初学者,在开发中遇到了Ajax分页的问题。
google了一下,在rails下面有很好用的分页插件will_paginate。
但是不支持Ajax。我用的版本是mislav-will_paginate-2.3.11。

还是Google……,发现了下面的代码:

class AjaxLinkRenderer < WillPaginate::LinkRenderer
  def page_link_or_span(page, span_class = 'current', text = nil)
    text ||= page.to_s
    classnames = Array[*span_class] 
    if page and page != current_page
      if @options[:update]
        @template.link_to_remote text, :update => @options[:update], :url => url_for(page)
      else
        @template.link_to text, url_for(page), :rel => rel_value(page), :class => classnames[1]
      end
    else
      @template.content_tag :span, text, :class => classnames.join(' ')
    end
  end
end

 

嗯,很好用。在config\initializers下面做成一个.rb。页面上的Ajax分页就可以用了。页面上的处理是下面的代码:

<%= will_paginate @producttradeinfos, 
        {:class=>'pagination', 
          :prev_label => '上一页', 
          :next_label => '下一页', 
          :renderer => 'AjaxLinkRenderer', 
          :update => 'results'}%>

 

但是,问题还是出现了,分页页面刷新时的查询是需要参数的,所以我要在Page连接候面加上查询的参数,传递给刷新后的页面,否则查询SQL将无法正常运行。

这个问题总觉得应该很好解决的,但是因为是初学还这不知道如何下手。请Rails高手们指点一下吧!谢谢了

把查询参数传给 params 选项:

[code="ruby"]<%= will_paginate @producttradeinfos,
{:class=>'pagination',
:prev_label => '上一页',
:next_label => '下一页',
:renderer => 'AjaxLinkRenderer',
:update => 'results',
:params => {你的参数(如: :q => params[:q])}}%>[/code]