Ethan
Ethan

Reputation: 60089

How can I get will_paginate to correctly display the number of entries?

WillPaginate has a page_entries_info view helper to output text like "Displaying contracts 1 - 35 of 4825 in total".

However, I'm finding that when I try to use it like this...

= page_entries_info @contracts

It outputs...

Displaying Contract 1 - 35 of 4825 in total

(It outputs the singular name of the model, rather than pluralized, all lower case.)

Do I need to feed it some other param?

I tried page_entries_info @contracts, :model => Contract but got the same result.

I'm using version 3.0.3 -- the current version.

Incidentally, can someone point me to the API docs for WillPaginate?

Upvotes: 5

Views: 1095

Answers (1)

Steve
Steve

Reputation: 7098

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

Short answer

You can specify the model option as a string, which will be correctly pluralized.

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

Longer answer

The will_paginate docs suggest that you use the i18n mechanism for customizing output. This is kind of a pain since AFAICT you have to write out singular and plural form for all of your models in the config/locales/*.yml files (e.g., en.yml), and the %{foo}-style syntax doesn't seem to be ERB, but just placeholders, so you can't do things like %{foo.downcase}.

If you write your own helper, you get complete control over the output. For example:

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

Upvotes: 4

Related Questions