我正在尝试按字母顺序排列 - 因此我的指南将按字母顺序列出(并分组),顶部的分页将按字母顺序排列。到目前为止,我有:
guidelines_controller.rb
def index
if params[:search].present?
@search = Sunspot.search(Guideline) do
fulltext params[:search]
end
@guidelines = @search.results
else
@guidelines = Guideline.order(:title).page(params[:page]).per(5).to_a.group_by{|u|u.title[0].upcase}
end
index.html视图
<%= paginate @guidelines %>
<% @guidelines.keys.sort.each do |starting_letter| %>
<h3>
<%= link_to starting_letter, {:action => :browse, :controller =>:index, :letter => starting_letter } %>
</h3>
<% @guidelines[starting_letter].each do |guideline| %>
然后是视图的其余部分
这给了我错误
ActionView::Template::Error (undefined method `current_page' for #<Hash:0x007fd974700148>):
1: <%= paginate @guidelines %>
2:
3: <div class="page-header">
4: <% if @search %>
app/views/guidelines/index.html.erb:1:in `_app_views_guidelines_index_html_erb___4442223584034894893_70285969096020'
app/controllers/guidelines_controller.rb:25:in `index'
我是否接近解决方案?感谢
答案 0 :(得分:1)
您可以使用kaminaris数组包装类:https://github.com/amatsuda/kaminari#paginating-a-generic-array-object
所以这一行可能如下所示:
@guidelines = Kaminari.paginate_array(Guideline.order(:title).to_a.group_by{|u|u.title[0].upcase}).page(params[:page]).per(5)
注意:这是完全未经测试的,并且不在我的头顶。