我在我的ROR项目中使用will_paginate gem来显示页面中的记录。
我想要下载页面,而不是使用ajax重新加载整个页面。
我在网上发现了一些例子,但对我来说并不奏效。
怎么办?
创建一个新的帮手(例如app / helpers / will_paginate_helper.rb),其中包含以下内容:
module WillPaginateHelper
class WillPaginateJSLinkRenderer < WillPaginate::ActionView::LinkRenderer
def prepare(collection,options,template)
options[:params] ||= {}
options[:params]['_'] = nil
super(collection,template)
end
protected
def link(text,target,attributes = {})
if target.is_a? Fixnum
attributes[:rel] = rel_value(target)
target = url(target)
end
@template.link_to(target,attributes.merge(remote: true)) do
text.to_s.html_safe
end
end
end
def js_will_paginate(collection,options = {})
will_paginate(collection,options.merge(:renderer => WillPaginateHelper::WillPaginateJSLinkRenderer))
end
end
然后在您的视图中使用此标记进行ajax分页:
<%= js_will_paginate @recipes %>
请记住,分页链接将包括URL的现有参数,您可以排除以下内容。这是标准的分页功能:
<%= js_will_paginate @recipes,:params => { :my_excluded_param => nil } %>
希望能解决你的问题。
更新1:添加了一个解释,这是如何工作的original question我发布这个解决方案。
更新2:我已经使Rails 4与remote:true链接兼容,并将帮助方法重命名为js_will_paginate。