Robin van Dijk
Robin van Dijk

Reputation: 827

Shorter way to render certain text

In a view I would like to render the amount of people living in a person's house. The follow code works, but I believe there is a shorter and better way to write the code.

<% if current_user.family_size == 1 %>
   <li><%= current_user.family_size %> person</li>
<% else %>
   <li><%= current_user.family_size %> people</li>
<% end %>

Thanks in advance.

Upvotes: 0

Views: 36

Answers (1)

BroiSatse
BroiSatse

Reputation: 44685

Use pluralize method:

<li><%= pluralize(current_user.family_size, 'person') %></li>

UPDATE:

If there is a need for pluralized noun without a count, use:

'person'.pluralize(2)   #=> 'people'

Upvotes: 2

Related Questions