ruby-on-rails - 如何让 will_paginate 正确显示条目数?

标签 ruby-on-rails ruby will-paginate

WillPaginate有一个 page_entries_info View 助手来输出文本,例如“显示契约(Contract) 1 - 35 of 4825 in total”。

但是,当我尝试像这样使用它时,我发现...

= page_entries_info @contracts

它输出...

Displaying Contract 1 - 35 of 4825 in total

(它输出模型的单数名称,而不是复数形式,全部小写。)

我需要给它一些其他参数吗?

我尝试了 page_entries_info @contracts, :model => Contract 但得到了相同的结果。

我使用的是 3.0.3 版——当前版本。

顺便说一句,有人可以指点我 WillPaginate 的 API 文档吗?

最佳答案

will_paginate API 文档:https://github.com/mislav/will_paginate/wiki/API-documentation

简答

您可以将模型选项指定为字符串,它将被正确复数化。

page_entries_info @contracts, :model => 'contract'
# Displaying contracts 1 - 35 of 4825 in total

更长的答案

will_paginate docs suggest你使用 the i18n mechanism用于自定义输出。这有点痛苦,因为 AFAICT 您必须在 config/locales/*.yml 文件(例如 en.yml)中为所有模型写出单数和复数形式,并且 %{foo}-style 语法似乎不是 ERB,而只是占位符,所以你不能做像 %{foo.downcase} 这样的事情。

如果您编写自己的帮助程序,您可以完全控制输出。例如:

def my_page_info(collection)
  model_class = collection.first.class
  if model_class.respond_to? :model_name
    model = model_class.model_name.human.downcase
    models = model.pluralize
    if collection.total_entries == 0
      "No #{models} found."
    elsif collection.total_entries == 1
      "Found one #{model}."
    elsif collection.total_pages == 1
      "Displaying all #{collection.total_entries} #{models}"
    else
      "Displaying #{models} #{collection.offset + 1} " +
      "to #{collection.offset + collection.length} " +
      "of #{number_with_delimiter collection.total_entries} in total"
    end
  end
end

# Displaying contracts 1 - 35 of 4,825 in total

关于ruby-on-rails - 如何让 will_paginate 正确显示条目数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11836854/

相关文章:

ruby - Watir - 拖放不起作用

ruby-on-rails - Will_Paginate - 如何获取所有分页对象 ID 的数组?

ruby-on-rails - RoR 从云迁移到载波

javascript - Rails "Load more..."而不是分页

ruby - gem install mongrel 使用 ruby​​ 1.9.1 失败

ruby-on-rails - 将分页 : How change default page in links

ruby-on-rails - 如何更新 Gem 上的单个提交?

ruby-on-rails - Rails session 不会持续

ruby-on-rails - 错误 : Could not find a valid gem 'rails' (>= 0) in any repository

ruby - 在 ruby 法拉第中保持活力