Simplifying Rails views with pluralize
"2 User(s) been updated."This is a silly problem that I kept running into before I found out about
pluralize
.Now, instead of having to do a conditional checking for number of users and printing the line based on that number, I can do this using
pluralize
in Rails:
pluralize(@users, 'User')
Or, if I want to use the Ruby version of
pluralize
(or, more accurately, the ActiveSupport version):
require 'active_support/inflector'
"User".pluralize(@users)
If
@users
is more than one, outputs "Users". If @users
is 1, outputs "User". This is nice for when you don't want to display the number, just the pluralized word (i.e. "Users updated").