51224bdd17878b3b19e8987e9bb336a2

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

_rating.rhtml

1
2
3
4
5
6
 <% if @resource.favorites.length > 1 %>
    Favorites
 <% else %>
    Favorite
 <% end %>

Refactorings

No refactoring yet !

1e8f141e7857d397d8020ed3b759e88a

Maciej Piechotka

August 19, 2008, August 19, 2008 08:09, permalink

1 rating. Login to rate!

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) %>
A8d3f35baafdaea851914b17dae9e1fc

Adam

August 19, 2008, August 19, 2008 14:16, permalink

1 rating. Login to rate!
1
<%= pluralize(@resource.favorites.length, "Favorite") %> 
4418c10276dac6242f80773cf9445d39

Jake

August 19, 2008, August 19, 2008 21:37, permalink

2 ratings. Login to rate!

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') %>

Your refactoring





Format Copy from initial code

or Cancel