pluralize without count number in rails 4

NothingToSeeHere picture NothingToSeeHere · Dec 2, 2014 · Viewed 12.4k times · Source

I am building a blog app. I'd like to be able to pluralize the word "article" if more than one "post" is "published."

Like so: Available Articles or Available Article

This is what I have....

 Available <%=  pluralize @posts.published, "Article" %>:

I've tried

 Available <%=  pluralize @posts.published.count, "Article" %>:

and that works...but I don't want the number. It shouldn't read Available 5 Articles....it should have no number.

Answer

jessewmc picture jessewmc · Feb 19, 2015

I have been looking for the answer to this myself and wasn't satisfied with any of the existing ones. Here's the tidiest solution I found:

 Available <%=  "Article".pluralize(@posts.published.count) %>:

Documentation is here. Relevant bits:

Returns the plural form of the word in the string.

If the optional parameter count is specified,
the singular form will be returned if count == 1.
For any other value of count the plural will be returned.

  'post'.pluralize             # => "posts"
  'apple'.pluralize(1)         # => "apple"
  'apple'.pluralize(2)         # => "apples"