加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 02:18:36 所属栏目:百科 来源:网络整理
导读:WillPaginate有一个page_entries_info视图助手输出文本,如“总共显示合同1 – 35 of 4825”. 但是,我发现当我尝试使用它时…… = page_entries_info @contracts 它输出…… Displaying Contract 1 – 35 of 4825 in total (它输出模型的单数名称,而不是复数,
WillPaginate有一个page_entries_info视图助手输出文本,如“总共显示合同1 – 35 of 4825”.

但是,我发现当我尝试使用它时……

= page_entries_info @contracts

它输出……

Displaying Contract 1 – 35 of 4825 in total

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

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

我试过了page_entries_info @contracts,:model =>合同但得到了相同的结果.

我使用的是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

更长的答案

您使用the i18n mechanism定制输出的will_paginate docs suggest.这是一种痛苦,因为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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读