will_paginate and ajax in rails 3

在rails3项目中出现分页内容不能加载Ajax情况,开始以为是升级rails 3的插件问题,记录一下解决办法。
1.view 显示层(如ask/show.html.erb)
< div id="queue"> < %= render :partial => ‘queue’ %>
< script>
$(document).ready(function() {
$(‘.pagination a’).attr(‘data-remote’, ‘true’);
});

2.controller (如ask_controller.rb)

def show
@user = User.find(params[:id])
@queue_items = @user.queue_items.with_state(:pending).paginate(:page => params[:queue_page] || 1, :per_page => 1)
respond_to do |format|
format.html
format.js
end
end

3.js template

$(‘#queue’).html(‘< %=escape_javascript render :partial => “queue” %>’);
$(‘.pagination a’).attr(‘data-remote’, ‘true’);

还有一种在Rails3中的方法也一并记录如下。(via:火哥的零碎)

class RemoteLinkRenderer < WillPaginate::ViewHelpers::LinkRenderer
def prepare(collection, options, template)
@remote = options.delete(:remote) || { }
super
end

def link(text, target, attributes = {})
if target.is_a? Fixnum
attributes[:rel] = rel_value(target)
target = url(target)
end
attributes[:href] = target
# page_attr = { :page => target }
@template.link_to( text.to_s.html_safe, target, :remote => true )
end
end

One thought on “will_paginate and ajax in rails 3

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>