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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 12/17/06, frank <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Your best way is to use each_with_index, for example <% @users.each_with_index do |user, index| %> User #<%= index+1 %>. <%= user.username %><br /> <% end %> That seems to work well for me :) On 12/17/06, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On 12/17/06, frank <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 %><br /> > <% end %> > > That seems to work well for me :)Thanks guys, I appreciate it. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---