Search code examples
ruby-on-railspluralize

Formatted pluralize


I have a case where I need to use pluralize to properly spell something. However, I need to render the html like so:

<span>1</span> thing

or,

<span>3</span> things

I could write a helper method, but I'm just making sure there isn't something in the box to do this.


Solution

  • In the interim, I've created this helper method, because it looks like there isn't what I'm looking for:

    def pluralize_word(count, singular, plural = nil)
      ((count == 1 || count == '1') ? singular : (plural || singular.pluralize))
    end
    

    It's essentially identical to the pluralize method, except that it removes the number from the front. This allows me to do this (haml):

    %span.label= things.size.to_s
    %description= pluralize_word(things.size, 'thing')