在rails中的will_paginate的page_entries_info中提供自定义消息

Par*_*ngh 7 ruby ruby-on-rails will-paginate

我是铁杆新手.我想为page_entries_info显示我的自定义消息.我已经通过以下链接但不能理解.任何人都可以详细解释.

怎么办-I-指定定制-措辞-IN-A-意志PAGINATE视辅助

Alp*_*nar 8

这是默认加载的,取自项目维基

en:
  will_paginate:
    page_entries_info:
      single_page:
        zero:  "No %{model} found"
        one:   "Displaying 1 %{model}"
        other: "Displaying all %{count} %{model}"
      single_page_html:
        zero:  "No %{model} found"
        one:   "Displaying <b>1</b> %{model}"
        other: "Displaying <b>all&nbsp;%{count}</b> %{model}"

      multi_page: "Displaying %{model} %{from} - %{to} of %{count} in total"
      multi_page_html: "Displaying %{model} <b>%{from}&nbsp;-&nbsp;%{to}</b> of <b>%{count}</b> in total"
Run Code Online (Sandbox Code Playgroud)

您需要更改multi_page_htmlmulti_page,最后2项.

在你的en.yml文件(或其他任何东西)中放置如下内容:

en:
  will_paginate:
    line_item:
      page_entries_info:
        multi_page: "Displaying %{from} - %{to} of %{count} of %{model}"        
        multi_page_html: "Displaying <b>%{from}&nbsp;-&nbsp;%{to}</b> of <b>%{count}</b> of %{model}"
Run Code Online (Sandbox Code Playgroud)

如果你有关于yml文件rails的困难,i18n指南有点高级但是提供了关于如何使用yml文件的很好的信息 - 只需向下滚动一点:).

我希望它有所帮助.


atm*_*ish 8

另一种选择是你可以page_entries_info()在你的方法中定义你的方法ApplicationHelper 并像往常一样使用它.如果您知道不需要覆盖边缘情况(如我的情况),这将为您提供更大的灵活性,甚至可以更加清洁和高效.您可以在此处参考原始方法定义,并查看您需要实现的所有内容.以下代码将运行您的大部分问题!

def page_entries_info(collection, options = {})
  entry_name = options[:entry_name] || (collection.empty?? 'item' :
      collection.first.class.name.split('::').last.titleize)
  if collection.total_pages < 2
    case collection.size
    when 0; "No #{entry_name.pluralize} found"
    else; "Displaying all #{entry_name.pluralize}"
    end
  else
    %{Displaying %d - %d of %d #{entry_name.pluralize}} % [
      collection.offset + 1,
      collection.offset + collection.length,
      collection.total_entries
    ]
  end
end
Run Code Online (Sandbox Code Playgroud)