我在rails 3中使用will_paginate 3.0.pre2进行分页
简单的分页,没有ajax它的工作正常,但当我尝试使链接ajaxable他们不工作。当我点击pagenumbers没有任何反应。
我有文章和评论模型。评论模型与文章和论坛有多态关联。对于评论我正在使用分页。
文章控制器 - 我添加了必需的分页代码:
def show
@article = Article.find(params[:id])
@comments = @article.comments.paginate(:per_page => 5, :page => params[:page])
@commentid = @article.id
@commenttype = @article.class
respond_to do |format|
format.html # index.html.erb
format.js
end
end
评论控制器:
def index
@comments = Comment.all..paginate(:per_page => 5, :page => params[:page])
respond_to do |format|
format.html # index.html.erb
format.js
end
end
def show
@comment = Comment.find(params[:id])
end
application.js:
$("#comments_pagination .pagination a").live("click", function() {
$.get("/articles/?"+this.href.split("?")[1], null, null, "script");
return false
});
comments / index.html.haml:
%hr
%h2 Say something!
= form_for([@commentable,Comment.new], :remote => true) do |f|
= f.label :commentcontent, "Comment here:"
= f.text_field :commentcontent
= f.hidden_field :user_id, :value => @current_user.id
= f.hidden_field :commentable_id, :value=> @commentid
= f.hidden_field :commentable_type, :value=> @commenttype
= f.submit "Add comment"
#addcomment
= render :template => 'comments/_pagelink'
_pagelink.html.haml:
#comments_pagination
= will_paginate(@comments,:renderer => RemoteLinkRenderer)
%h1 All comments
#comments
= render :partial=>@comments,:locals=>{:list=>true}
remote_link_renderer:
class RemoteLinkRenderer < WillPaginate::ViewHelpers::LinkRenderer
def link(text, target, attributes = {})
attributes["data-remote"] = true
super
end
end
index.js.erb的:
$("#addcomment").html("<%= escape_javascript(render :template => 'comments/_pagelink') %>");