将 Rails 分页 3 每页
我试图限制使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最终被迫将每页限制移至查询中。似乎是 Rails 3 版本的一个错误。因此,固定使用:
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:
@Kevin,如果您想确保 per_page 在各种查询中保持一致,您可以使用 Company.per_page,例如。
您也可以尝试 Kaminari gem,它与 Rails 3 集成得更好: http:// /railscasts.com/episodes/254-pagination-with-kaminari
@Kevin, if you want to be sure per_page is consistent across various queries you can use Company.per_page, eg.
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
为什么您使用“公司”而不是“公司”。这可能只是一个错字,但它似乎是一个问题。
Why are you using 'Companies' and not 'Company'. This might just be a typo here but it appears to be an issue.