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

就像这样:

这就是我的....

 Available <%=  pluralize @posts.published, "Article" %>:

我试过了

 Available <%=  pluralize @posts.published.count, "Article" %>:

这很有效...但我不想要号码.它不应该阅读可用的5篇文章....它应该没有号码.

推荐答案

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

 Available <%=  "Article".pluralize(@posts.published.count) %>:

文件是here.相关位:

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

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"

Ruby-on-rails相关问答推荐

如何在嵌套路由中使用Ruby on rails多态关联

在Ruby on Rails中获取堆栈级别太深错误(&Q)

Rails 7 中的预编译

具有 IN 子句的参数化查询在 ruby​​-on-rails 的 blazer 工具中无法按预期工作

Rails 控制台 - 更新 Gem 后的弃用通知

Ruby on rails 查询不知道怎么写

如何获取 Rails 表单构建器将为某个字段生成的 HTML名称属性?

OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=SSLv3 read server hello A

如何在 Rails 迁移中添加判断约束?

是否有与 PHP 的 isset() 等效的 Rails?

奇怪的406 不可接受错误

使用 jQuery 在 Rails 中不显眼的动态表单字段

Rails 3 中的猴子补丁

如何拯救 OmniAuth::Strategies::OAuth2::CallbackError?

在 rails 3 中批量插入

有没有办法列出所有 belongs_to 关联?

Rails 3.1 中的 Rails.cache 错误 - TypeError: can't dump hash with default proc

将现有的 html.erb 转换为 Haml

Rails:如何将日期时间字符串解析为特定时区

我如何为 Rails3/Bundler 供应 gems