Pluralization in Rails View Tasks

I have a question about the pluralization function. In my opinion, I have the following line of code. It passes an item with a certain number of votes to determine if it should be pluralized.

<%= pluralize(item.votes, 'Vote') %> 

My problem is that my opinion is conveyed by the word β€œVoices”, followed by a certain number of votes (item.votes). I just want him to convey the word "Voices". Ideas are greatly appreciated.

+4
source share
3 answers

You can do:

 pluralize(items.votes, 'Vote').split(" ", 2)[1] 

Hope this helps!

+3
source

You can make it easier:

 "Vote".pluralize(item.votes) 
+8
source

You can create your own method in the helper.

 def pluralize_without_count(string, count) count == 1 ? string : string.pluralize end 

and use it in your view:

 <%= pluralize_without_count('Vote', item.votes) %> 
+2
source

Source: https://habr.com/ru/post/1391017/


All Articles