将 Rails 分页 3 每页

发布于 2024-09-06 23:04:17 字数 570 浏览 5 评论 0原文

我试图限制使用Rails 3 mislav's will paginate 返回的元素数量。我目前正在使用:

# Gemfile
gem 'will_paginate', :git => 'git://github.com/mislav/will_paginate.git', :branch => 'rails3'

# company.rb
class Company < ActiveRecord::Base
  self.per_page = 8
end

# company_controller.rb
def index
  @companies = Company.where(...).paginate(:page => params[:page])
end

这会分页,但不是每页 8 个项目。如果我修改代码以不使用“where”,它就可以正常工作。然而,添加“where”或“scoped”似乎会引起问题。有什么想法我做错了吗?

谢谢。

I am trying to limit the number of elements returned with mislav's will paginate with Rails 3. I am currently using:

# Gemfile
gem 'will_paginate', :git => 'git://github.com/mislav/will_paginate.git', :branch => 'rails3'

# company.rb
class Company < ActiveRecord::Base
  self.per_page = 8
end

# company_controller.rb
def index
  @companies = Company.where(...).paginate(:page => params[:page])
end

This does pagination, but not 8 items per page. If I modify the code to not use the "where" it works fine. However, adding "where" or "scoped" seems to cause issues. Any ideas what I'm doing wrong?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

稍尽春風 2024-09-13 23:04:17

最终被迫将每页限制移至查询中。似乎是 Rails 3 版本的一个错误。因此,固定使用:

@companies = Company.where(...).paginate(:page => params[:page], :per_page => 8)

Ended up being forced to move the per page limit into the query. Appears to be a bug with the Rails 3 version. Thus, fixed using:

@companies = Company.where(...).paginate(:page => params[:page], :per_page => 8)
中二柚 2024-09-13 23:04:17

@Kevin,如果您想确保 per_page 在各种查询中保持一致,您可以使用 Company.per_page,例如。

@companies = Company.where(...).paginate(:page => params[:page], :per_page => Company.per_page)

您也可以尝试 Kaminari gem,它与 Rails 3 集成得更好: http:// /railscasts.com/episodes/254-pagination-with-kaminari

class Company < ActiveRecord::Base
  paginates_per 7
end

@companies = Company.where(...).page(params[:page])

@Kevin, if you want to be sure per_page is consistent across various queries you can use Company.per_page, eg.

@companies = Company.where(...).paginate(:page => params[:page], :per_page => Company.per_page)

You may also give a try to Kaminari gem which is much better integrated with rails 3: http://railscasts.com/episodes/254-pagination-with-kaminari

class Company < ActiveRecord::Base
  paginates_per 7
end

@companies = Company.where(...).page(params[:page])
獨角戲 2024-09-13 23:04:17

为什么您使用“公司”而不是“公司”。这可能只是一个错字,但它似乎是一个问题。

Why are you using 'Companies' and not 'Company'. This might just be a typo here but it appears to be an issue.

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