Ruby/Rails - kaminari 未定义方法分页错误

发布于 2024-11-27 17:33:47 字数 795 浏览 2 评论 0原文

我不确定我做了什么,但 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

可爱暴击 2024-12-04 17:33:48

要解决此问题,请包含 gem 'kaminari' 并删除 will_paginate。由于我已经在使用 will_paginate,因此我刚刚将当前的 will_paginate 调用更新为 kaminari。它们的实施非常相似,并且很容易更改。

To solve the problem, include gem 'kaminari' and remove will_paginate. Since I have already been using will_paginate, I just updated my current will_paginate calls to kaminari. They are very similar to implement and easy enough to change.

一抹淡然 2024-12-04 17:33:48

我尝试了 johnnycakes 的解决方案,但它一直在仪表板上给我堆栈级别太深的错误(类似于 https: //github.com/gregbell/active_admin/issues/157

我找到的解决方案是指定此修订版:

gem 'activeadmin', :git => 'git://github.com/gregbell/active_admin.git', :ref => '811f286fda3b6dfa91aa'

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:

gem 'activeadmin', :git => 'git://github.com/gregbell/active_admin.git', :ref => '811f286fda3b6dfa91aa'
绿萝 2024-12-04 17:33:47

嗯,刚刚想通了。
我安装了Active Admin。它安装了 will_paginate 作为依赖项。

Active Admin 的最新提交中,will_paginate 已替换为 kaminari

我更改了 Gemfile 以从 github 中提取 Active Adminwill_paginate 已从我的包中删除,现在一切正常。您可以通过将以下行放入 gemfile 中来完成此操作:

gem "activeadmin", git: "https://github.com/gregbell/active_admin"

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 with kaminari.

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:

gem "activeadmin", git: "https://github.com/gregbell/active_admin"
狼性发作 2024-12-04 17:33:47

我对另一个需要 will_paginate 的 gem 也有同样的问题。此问题已通过以下代码片段解决,该代码片段取自 active_admin wiki< /a> page:

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

将其放入初始化程序中。

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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文