任何人都可以建议我如何在rails 2.3.8中使用带有ajax的will_paginate 我应该在ClientController中的以下代码行中进行哪些更改
def index
@clients = Client.paginate(:per_page => 5, :page => params[:page])
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @clients }
end
end
并在索引视图文件中
<%= will_paginate @clients %>
答案 0 :(得分:1)
will_paginate没有本机AJAX支持,但确实提供了切换链接渲染。将以下类添加到RAILS_ROOT / lib中的remote_link_renderer.rb,以启用适合AJAX回调的链接的呈现视图:
class RemoteLinkRenderer < WillPaginate::LinkRenderer
def prepare(collection, options, template)
@remote = options.delete(:remote) || {}
super
end
protected
def page_link(page, text, attributes = {})
@template.link_to_remote(text, {:url => url_for(page), :method => :get}.merge(@remote))
#replace :method => :post if you need POST action
end
end
在视图中使用此选项来呈现分页链接(根据需要自定义):
<%= will_paginate(@results, :params => {:controller => :mycontroller, :action => :search, :query => h(@query)}, :renderer => RemoteLinkRenderer, :page_links => false) %>
控制器变得有点棘手,因为您需要替换呈现这些分页链接和结果的视图。我使用过这样的东西:
if request.xhr? or request.post?
if params[:page]
render :update do |page|
page.replace_html :search_results, :partial => 'mycontroller/search_results', :layout => false
end
else
render :partial => 'mycontroller/search_results'
end
else
# Render some normal content here
end