我有一个例子,我需要使用复数来正确拼写某些内容。但是,我需要像这样渲染 html:

<span>1</span> thing

或者,

<span>3</span> things

我可以编写一个辅助方法,但我只是确保框中没有任何东西可以执行此操作。

有帮助吗?

解决方案 2

在此期间,我创建这个辅助方法,因为它看起来像没有我要找的:

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

其他提示

这使用了 Rails 类 文本助手 它使用 偏转器 如果需要的话进行复数化。

def pluralize_with_html(count, word)
  "<span>#{count}</span> #{TextHelper.pluralize(count, word)}"
end
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top