7
8

More than 3 years have passed since last update.

【Rails】pluralize

Posted at

pluralizeヘルパー

英語専用のテキストヘルパー。
最初の引数に整数があると、それに基づいて2番目の引数である英単語が複数形に変化したものが渡される。

サンプルコード

>> pluralize(2, "woman")
=> "2 women"
>> pluralize(3, "erratum")
=> "3 errata"

実施例

<%= pluralize(@user.errors.count, "error") %>

このコードだと"0 errors"、"1 error"、"2 errors"などのように、エラーの数によって活用される単語が変わってきます。"1 errors" のような英語の文法に合わない文字列を避けることができます

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8