Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Give custom messages in page_entries_info of will_paginate in rails

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

like image 555
Paritosh Singh Avatar asked Aug 01 '12 10:08

Paritosh Singh


2 Answers

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
like image 131
atmaish Avatar answered Nov 15 '22 15:11

atmaish


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.

like image 44
Alper Karapınar Avatar answered Nov 15 '22 16:11

Alper Karapınar