Give custom messages in page_entries_info of will_paginate in rails

空扰寡人 提交于 2019-12-06 23:58:21

问题


I am new to rails. I want to display my custom message for page_entries_info. I have gone through following link but cannot understand much. Can anyone please explain in details.

how-do-i-specify-custom-wording-in-a-will-paginate-view-helper


回答1:


Another option is you can define your page_entries_info() method in your ApplicationHelper and use it as you normally would. This would give you more flexibility and can even be more cleaner and efficient if you know that you dont need to cover the edge cases (as in my case). You can refer the original method definition here and see what all you need to implement. Following code would run for most part of your problem!

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



回答2:


This is what is loaded by default, taken from project wiki

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"

you need to change multi_page_html and multi_page, the last 2 entries.

in your en.yml file (or whatever it is) put something like this:

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}"

If you have difficulties about yml file rails i18n guide is a little advanced but gives nice information about how to use yml file - just scroll down a little :).

I hope it helps.



来源:https://stackoverflow.com/questions/11757726/give-custom-messages-in-page-entries-info-of-will-paginate-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!