【问题标题】:How to use pagination in rails 3.2.3?如何在 Rails 3.2.3 中使用分页?
【发布时间】:2012-06-03 04:48:41
【问题描述】:

我是 RoR 的新手,我想在我的应用程序中加入分页功能。

执行此任务的最佳方式是什么?请给我建议。是否可以在 rails 3.2.3 中使用 will_paginate gem ?

如果是,我应该包括哪种方法:

Post.where(:published => true).paginate(:page => params[:page]).order('id DESC')  

Post.page(params[:page]).order('created_at DESC')    

【问题讨论】:

    标签: ruby-on-rails will-paginate


    【解决方案1】:

    是的,可以在 rails 3.2.3 应用程序中使用 will_paginate gem。

    并且代码将取决于每个操作将执行的操作。

    下面的代码通常在你的PostsController 中使用#index 方法。

    @posts = Post.where(:published => true).paginate(:page => params[:page]).order('id DESC')
    

    因此,在您的posts/index.html.erb 视图文件中,您可以使用以下代码来显示分页链接:

    <%= will_paginate @posts %>
    

    【讨论】:

    • Kaminari 是另一种主流解决方案。
    • 感谢您的回复。但是当我使用这个 coed 时,会出现以下错误: undefined mentho 'paginage' for #<:relation:0x....>
    • 那是因为方法调用paginate,而不是paginage
    • 确保您已在您的Gemfile 中添加gem 'will_paginate' 并在您的终端运行命令bundle install
    • 不,您无需向model 添加任何内容即可使其正常工作。
    猜你喜欢
    • 1970-01-01
    • 2012-11-27
    • 2012-06-04
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    相关资源
    最近更新 更多