_rating.rhtml
1 2 3 4 5 6
<% if @resource.favorites.length > 1 %> Favorites <% else %> Favorite <% end %>
Refactorings
No refactoring yet !
Maciej Piechotka
August 19, 2008, August 19, 2008 08:09, permalink
Only in the i18n/l10n frameworks.
However it allows you keep things dry:
Helper
1 2 3 4 5 6 7 8
def pluralize(string, length) if length > 1 string.pluralize else string end end
View
1 2
<%= pluralize("Favorites", @resource.favorites.length) %>
Adam
August 19, 2008, August 19, 2008 14:16, permalink
1
<%= pluralize(@resource.favorites.length, "Favorite") %>
Jake
August 19, 2008, August 19, 2008 21:37, permalink
You can find the method to pluralize in the rails API under 'pluralize'.
Instance methods take time and resource to create, and I doubt the expense of creating one will show any significant increase in efficiency. At the same time, @resource.favorites.length is dead obvious what it is, whereas @fav_length is not, and requires looking for it to see what the value is.
1
<%= pluralize(@resource.favorites.length, 'Favorite') %>
Hi,
There is a rails function for doing the following thing, I totally forgot it.
Also one more thing I am writing ( @resource.favorites.length ) 3 times in my _rating rhtml file. Shall I write only once in controller
@fav_length = @resource.favorites.length and then use @fav_length instance variable in partial or is there any other better way to do so.
Thanks
DG
Thanks in advance