Question

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.

Was it helpful?

Solution 2

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')

OTHER TIPS

This uses the Rails class TextHelper which uses Inflector to do the pluralization if needed.

def pluralize_with_html(count, word)
  "<span>#{count}</span> #{TextHelper.pluralize(count, word)}"
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top