kaminari与ror to pagination

时间:2012-07-26 18:42:59

标签: ruby-on-rails kaminari

我第一次尝试与kaminari进行分页,我得到了错误:

views/store/index.html.erb where line #23 raised:

undefined method `current_page' for #<Array:0x3ebe0c0>

Extracted source (around line #23):

20:     </div>
21: </div>
22: <%end%>
23: <%= paginate @buildings %>

控制器&GT; buildings_controller

def index
    @buildings = Building.all.page(params[:page]).per(1)

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @buildings }
    end
  end

商店&GT;索引

<%= paginate @buildings %>

我做错了什么?

2 个答案:

答案 0 :(得分:1)

删除

中的all
@buildings = Building.all.page(params[:page]).per(1)

喜欢这个

@buildings = Building.page(params[:page]).per(1)

你很高兴 ActiveRecord all方法返回一个数组,我相信Kaminari不希望数组工作。

答案 1 :(得分:0)

我已经改变了kaminari,我使用了will_paginate并且像疯了一样工作!

宝石文件中的

gem'will_paginate','&gt; = 3.0.pre'

在dash =&gt;中捆绑安装

商店中的

&gt;索引

<%= will_paginate @buildings %>
控制器中的

&gt; store_controller

@buildings = Building.paginate:page =&gt; params [:page],:order =&gt;'created_at DESC',:per_page =&gt; 1

完成了!你喜欢用paginate