How do you set a variable in the view? I need to setup a counter, I
have a query that loops through its records, I need to set the count to
1 and increment the counter each time the loop occurs.
On 12/17/06, frank [email protected] wrote:
How do you set a variable in the view? I need to setup a counter, I
have a query that loops through its records, I need to set the count to
1 and increment the counter each time the loop occurs.
It would be a lot better just to just records.size, because the info’s
already there.
Anyway, you can do
<% counter = 1 %>
<% records.each do |r| %>
<% counter += 1 %>
… other stuff
<% end %>
Pat
Your best way is to use each_with_index, for example
<% @users.each_with_index do |user, index| %>
User #<%= index+1 %>. <%= user.username %>
<% end %>
That seems to work well for me
kris wrote:
Your best way is to use each_with_index, for example
<% @users.each_with_index do |user, index| %>
User #<%= index+1 %>. <%= user.username %>
<% end %>That seems to work well for me
Thanks guys, I appreciate it.