我有一个案例,我需要使用复数来正确拼写一些东西。但是,我需要像这样渲染 html:
<span>1</span> thing
或者,
<span>3</span> things
我可以编写一个辅助方法,但我只是确保盒子里没有东西可以做到这一点。
我有一个案例,我需要使用复数来正确拼写一些东西。但是,我需要像这样渲染 html:
<span>1</span> thing
或者,
<span>3</span> things
我可以编写一个辅助方法,但我只是确保盒子里没有东西可以做到这一点。
这使用 Rails 类TextHelper ,如果需要,它使用Inflector进行复数。
def pluralize_with_html(count, word)
"<span>#{count}</span> #{TextHelper.pluralize(count, word)}"
end
在此期间,我创建了这个辅助方法,因为它看起来没有我要找的东西:
def pluralize_word(count, singular, plural = nil)
((count == 1 || count == '1') ? singular : (plural || singular.pluralize))
end
它基本上与复数方法相同,只是它从前面删除了数字。这允许我这样做(haml):
%span.label= things.size.to_s
%description= pluralize_word(things.size, 'thing')