[英]Ruby/Rails - kaminari undefined method pagination errors
I'm not sure what I did, but kaminari has started acting weird in my app. 我不知道我做了什么,但是kaminari已经开始在我的应用程序中表现得很奇怪了。
In my controller: 在我的控制器中:
@producers = Producer.order(:name).page(params[:page])
view: 视图:
<%= paginate @producers %>
results in: 结果是:
undefined method `num_pages' for #<ActiveRecord::Relation:0x000001026e6308>
If I add .per in my controller: 如果我在我的控制器中添加.per:
@producers = Producer.order(:name).page(params[:page]).per(25)
I get 我明白了
undefined local variable or method `per' for #<ActiveRecord::Relation:0x0000010928ef60>
Finally, strangely, if I move my .order(:name)
to the end, it works: 最后,奇怪的是,如果我将
.order(:name)
移动到最后,它可以工作:
@producers = Producer.page(params[:page]).order(:name)
I'm guessing some other gem I have installed has a page
scope or method that's causing problems? 我猜我安装的其他一些宝石有一个
page
范围或方法导致问题?
Thanks. 谢谢。
Well, just figured it out. 嗯,刚想通了。 I had Active Admin installed.
我安装了Active Admin 。 It installed
will_paginate
as a dependency. 它将
will_paginate
安装为依赖项。
In the latest commits for Active Admin
, will_paginate
has been replaced with kaminari
. 在
Active Admin
的最新提交中, will_paginate
已被kaminari
取代。
I changed my Gemfile to pull Active Admin
from github. 我改变了我的Gemfile以从github拉出
Active Admin
。 will_paginate
was removed from my bundle and now everything works. will_paginate
已从我的包中删除,现在一切正常。 You can do this by putting the following line into your gemfile: 您可以通过将以下行放入gemfile中来完成此操作:
gem "activeadmin", git: "https://github.com/gregbell/active_admin"
I had the same problem with another gem that required will_paginate. 我对另一个需要will_paginate的gem有同样的问题。 The issue was resolved with this code snippet which was taken from active_admin wiki page:
使用此代码片段解决了该问题,该代码段取自active_admin wiki页面:
if defined?(WillPaginate)
module WillPaginate
module ActiveRecord
module RelationMethods
def per(value = nil) per_page(value) end
def total_count() count end
end
end
module CollectionMethods
alias_method :num_pages, :total_pages
end
end
end
Put it in an initializer. 把它放在初始化器中。
To solve the problem, include gem 'kaminari'
and remove will_paginate
. 要解决这个问题,请包含
gem 'kaminari'
并删除will_paginate
。 Since I have already been using will_paginate
, I just updated my current will_paginate calls to kaminari. 由于我已经使用了
will_paginate
,我刚刚更新了当前对kaminari的will_paginate调用。 They are very similar to implement and easy enough to change. 它们与实现非常相似,并且易于更改。
I tried johnnycakes's solution, but it kept giving me stack level too deep errors on the dashboard (similar to https://github.com/gregbell/active_admin/issues/157 ) 我尝试了johnnycakes的解决方案,但它一直给我在仪表板上的堆栈级别太深的错误(类似于https://github.com/gregbell/active_admin/issues/157 )
The solution I found was to specify this revision: 我找到的解决方案是指定此修订:
gem 'activeadmin', :git => 'git://github.com/gregbell/active_admin.git', :ref => '811f286fda3b6dfa91aa'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.