Rails3 - 使用 Kaminari + 进行排序和分页AJAX +不显眼的 JavaScript
我已经成功地用 Kaminari 设置了 ajax 分页。
为了添加排序功能,我遵循了这个伟大的 Railscast并将 will_paginate 替换为 Kaminari。
分页工作得很好,但排序只是第一次工作,因为 sort_column 和 sort_direction 没有更新。
我不明白为什么。
这是我的代码:
控制器:
def index
@questions = Question.order(sort_column + " " + sort_direction).page(params[:page])
end
....
private
def sort_column
Question.column_names.include?(params[:sort]) ? params[:sort] : "created_at"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
end
我添加了 :remote =>正确,:方法=> :get
到
ApplicationHelper :
def sortable(title = nil, column)
title ||= column.titleize
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, params.merge(:sort => column, :direction => direction), :remote => true, :method => :get
end
index.html.erb :
<%= sortable "created_at" %><br/>
<div id="questions"><%= render @questions %></div>
<div id="paginator"><%= paginate @questions, :remote => true %>
index.js.erb :
$('#questions').html('<%= escape_javascript(render @questions) %>');
$('#paginator').html('<%= escape_javascript(paginate(@questions, :remote => true).to_s) %>');
这是 :remote => 的问题吗?正确,:方法=> :get
部分?如果我删除它,排序可以工作,但没有ajax。
I have successfully set up an ajax pagination with Kaminari.
In order to add a sort feature I followed this great Railscast and replaced will_paginate by Kaminari.
The pagination works great but the sort works just the first time because the sort_column and the sort _direction are not updated.
I can't figure out why.
Here is my code :
Controller :
def index
@questions = Question.order(sort_column + " " + sort_direction).page(params[:page])
end
....
private
def sort_column
Question.column_names.include?(params[:sort]) ? params[:sort] : "created_at"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "desc"
end
I have added :remote => true, :method => :get
to
ApplicationHelper :
def sortable(title = nil, column)
title ||= column.titleize
direction = column == sort_column && sort_direction == "asc" ? "desc" : "asc"
link_to title, params.merge(:sort => column, :direction => direction), :remote => true, :method => :get
end
index.html.erb :
<%= sortable "created_at" %><br/>
<div id="questions"><%= render @questions %></div>
<div id="paginator"><%= paginate @questions, :remote => true %>
index.js.erb :
$('#questions').html('<%= escape_javascript(render @questions) %>');
$('#paginator').html('<%= escape_javascript(paginate(@questions, :remote => true).to_s) %>');
Is this a problem with the :remote => true, :method => :get
part ? If I remove it the sort works but without ajax.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://asciicasts.com/episodes/254-pagination-with-kaminari
http://asciicasts.com/episodes/254-pagination-with-kaminari