Ruby/Rails - kaminari 未定义方法分页错误
我不确定我做了什么,但 kaminari 开始在我的应用程序中表现得很奇怪。
在我的控制器中:
@producers = Producer.order(:name).page(params[:page])
view:
<%= paginate @producers %>
结果:
undefined method `num_pages' for #<ActiveRecord::Relation:0x000001026e6308>
如果我在控制器中添加 .per:
@producers = Producer.order(:name).page(params[:page]).per(25)
我得到
undefined local variable or method `per' for #<ActiveRecord::Relation:0x0000010928ef60>
最后,奇怪的是,如果我将 .order(:name)
移到最后,它会起作用:
@producers = Producer.page(params[:page]).order(:name)
I'我猜测我安装的其他一些 gem 的 page
范围或方法会导致问题?
谢谢。
I'm not sure what I did, but kaminari has started acting weird in my app.
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:
@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:
@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?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要解决此问题,请包含
gem 'kaminari'
并删除will_paginate
。由于我已经在使用will_paginate
,因此我刚刚将当前的 will_paginate 调用更新为 kaminari。它们的实施非常相似,并且很容易更改。To solve the problem, include
gem 'kaminari'
and removewill_paginate
. Since I have already been usingwill_paginate
, I just updated my current will_paginate calls to kaminari. They are very similar to implement and easy enough to change.我尝试了 johnnycakes 的解决方案,但它一直在仪表板上给我堆栈级别太深的错误(类似于 https: //github.com/gregbell/active_admin/issues/157)
我找到的解决方案是指定此修订版:
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)
The solution I found was to specify this revision:
嗯,刚刚想通了。
我安装了Active Admin。它安装了
will_paginate
作为依赖项。在
Active Admin
的最新提交中,will_paginate
已替换为kaminari
。我更改了 Gemfile 以从 github 中提取
Active Admin
。will_paginate
已从我的包中删除,现在一切正常。您可以通过将以下行放入 gemfile 中来完成此操作:Well, just figured it out.
I had Active Admin installed. It installed
will_paginate
as a dependency.In the latest commits for
Active Admin
,will_paginate
has been replaced withkaminari
.I changed my Gemfile to pull
Active Admin
from github.will_paginate
was removed from my bundle and now everything works. You can do this by putting the following line into your gemfile:我对另一个需要 will_paginate 的 gem 也有同样的问题。此问题已通过以下代码片段解决,该代码片段取自 active_admin wiki< /a> page:
将其放入初始化程序中。
I had the same problem with another gem that required will_paginate. The issue was resolved with this code snippet which was taken from active_admin wiki page:
Put it in an initializer.