在轨道4中没有计数的复数

Not*_*ere 47 activerecord ruby-on-rails pluralize ruby-on-rails-4

我正在构建一个博客应用程序.如果"发布"多个"帖子",我希望能够复制"文章"这个词.

像这样:可用文章或可用文章

这就是我拥有的......

 Available <%=  pluralize @posts.published, "Article" %>:
Run Code Online (Sandbox Code Playgroud)

我试过了

 Available <%=  pluralize @posts.published.count, "Article" %>:
Run Code Online (Sandbox Code Playgroud)

这工作......但我不想要这个号码.它不应该读取可用的5篇文章....它应该没有数字.

jes*_*wmc 79

我一直在寻找这个问题的答案,并且对现有的任何一个都不满意.这是我找到的最整洁的解决方案:

 Available <%=  "Article".pluralize(@posts.published.count) %>:
Run Code Online (Sandbox Code Playgroud)

文档在这里.相关位:

返回字符串中复数形式的单词.

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"
Run Code Online (Sandbox Code Playgroud)


Jak*_*b W 12

您可以使用Rails Internationalization(I18n)来完成此任务.在您config/data/en.yml的翻译中将是这样的:

en:
  available_articles:
    zero: Available Article
    one: Available Article
    other: Available Articles
Run Code Online (Sandbox Code Playgroud)

在您看来,您应该能够得到这样的翻译:

<%= t(:available_articles, count: @posts.published.count) %> 
Run Code Online (Sandbox Code Playgroud)