Okay, I know this will probably be a very easy question. But I am discovering that RonR makes just about everything easy. The problem is, it takes me a few hours to figure out that I can do it the easy way. I need to know how to move to the next record in a database. I want to format my out put in a table. For example, I want a 3 by 3 table. So record one goes in the first cell, record two goes in the middle top cell, etc. Thanks. -Ross -- Posted via http://www.ruby-forum.com/.
Ross: controller @record = Model.find(:all) -- record could have been ''x'' or anything else you like. :all can be limited using :conditions in the view you put <table> <tr> <th> header </th> @record.each do |r| <tr> <td><%=r.name%></td> etc.... </tr> <%end%> </table> On 16-Jan-06, at 11:26 PM, Ross Malaga wrote:> Okay, I know this will probably be a very easy question. But I am > discovering that RonR makes just about everything easy. The > problem is, > it takes me a few hours to figure out that I can do it the easy way. > > I need to know how to move to the next record in a database. I > want to > format my out put in a table. For example, I want a 3 by 3 table. So > record one goes in the first cell, record two goes in the middle top > cell, etc. > > Thanks. > > -Ross > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks for the reply, but that does not quite work. It puts everything in one row. But I need multiple rows and columns, like this: Record 1 Record 2 Record 3 Record 4 Record 5 Record 6 Record 7 Record 8 Record 9 In ASP I would use a MoveNext method and 2 For...Next loops to handle this. So, what is the RonR equivalent of MoveNext? Thanks. -Ross bruce balmer wrote:> Ross: > > controller > > @record = Model.find(:all) -- record could have been ''x'' or anything > else you like. :all can be limited using :conditions > > > in the view you put > > <table> > <tr> > <th> header </th> > > > @record.each do |r| > <tr> > <td><%=r.name%></td> > etc.... > > </tr> > > <%end%> > </table>-- Posted via http://www.ruby-forum.com/.
Well, how about this: @x = Model.find(:all) This produces an array of objects, one object for each row you will have pulled down. in the cells respectively, I would place Then each record you want would be in the appropriately numbered array element. ie. first record would be in @x[0], next record in @x[1] So I''d write two loops, one to go sideways ([0] [1] [2]) and one to go vertically ie [0] [3] [6] et voil?. Two loops counting through the array elements where @x = the array. bruce On 17-Jan-06, at 11:55 AM, Ross Malaga wrote:> Thanks for the reply, but that does not quite work. It puts > everything > in one row. But I need multiple rows and columns, like this: > > > Record 1 Record 2 Record 3 > > Record 4 Record 5 Record 6 > > Record 7 Record 8 Record 9 > > In ASP I would use a MoveNext method and 2 For...Next loops to handle > this. So, what is the RonR equivalent of MoveNext? > > Thanks. > > -Ross > > > > > bruce balmer wrote: >> Ross: >> >> controller >> >> @record = Model.find(:all) -- record could have been ''x'' or anything >> else you like. :all can be limited using :conditions >> >> >> in the view you put >> >> <table> >> <tr> >> <th> header </th> >> >> >> @record.each do |r| >> <tr> >> <td><%=r.name%></td> >> etc.... >> >> </tr> >> >> <%end%> >> </table> > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On 1/17/06, Ross Malaga <malagar@mail.montclair.edu> wrote:> Thanks for the reply, but that does not quite work. It puts everything > in one row. But I need multiple rows and columns, like this: > > > Record 1 Record 2 Record 3 > > Record 4 Record 5 Record 6 > > Record 7 Record 8 Record 9 > > In ASP I would use a MoveNext method and 2 For...Next loops to handle > this. So, what is the RonR equivalent of MoveNext?Not sure if there is an equivalent, but something like this may work: # For m columns (assuming records/length is cleanly divisible by m) <% 0.upto(@records.length/m-1) do |i| -%> <tr> <% 0.upto(m-1) do |j| -%> <td><%= @records[m*i+j] %></td> <% end %> </tr> <% end %>
Something like this would work with just one loop. Making it prettier is left as an exercise for the reader. -Rob <table> <% records = @Record.find(:all) per_row = 3 records.each_index { |position| if position.modulo(per_row).zero? -%> <tr> <% end -%> <td>Record <%= "#{position.next}: #{records[position].name}" %></td> <% if position.next.modulo(per_row).zero? -%> </tr> <% end } if ! records.length.modulo(per_row).zero? -%> </tr> <% end -%> </table> At 1/17/2006 01:55 PM, Ross Malaga wrote:>Thanks for the reply, but that does not quite work. It puts everything >in one row. But I need multiple rows and columns, like this: > > >Record 1 Record 2 Record 3 > >Record 4 Record 5 Record 6 > >Record 7 Record 8 Record 9 > >In ASP I would use a MoveNext method and 2 For...Next loops to handle >this. So, what is the RonR equivalent of MoveNext? > >Thanks. > >-Ross > > > > >bruce balmer wrote: > > Ross: > > > > controller > > > > @record = Model.find(:all) -- record could have been ''x'' or anything > > else you like. :all can be limited using :conditions > > > > > > in the view you put > > > > <table> > > <tr> > > <th> header </th> > > > > > > @record.each do |r| > > <tr> > > <td><%=r.name%></td> > > etc.... > > > > </tr> > > > > <%end%> > > </table> > > >-- >Posted via http://www.ruby-forum.com/. >_______________________________________________ >Rails mailing list >Rails@lists.rubyonrails.org >http://lists.rubyonrails.org/mailman/listinfo/rails-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060117/fbfe2b10/attachment.html
This is ugly, but lets you change the # of columns very easily: <% myCount = 0 numColumns = 3 @records.each do |record| if myCount % numColumns == 0 %><tr><% end %><td> <%= record %></td> if myCount == numColumns %></tr><% end end %> On 1/17/06, Jeremy Evans <jeremyevans0@gmail.com> wrote:> > On 1/17/06, Ross Malaga <malagar@mail.montclair.edu> wrote: > > Thanks for the reply, but that does not quite work. It puts everything > > in one row. But I need multiple rows and columns, like this: > > > > > > Record 1 Record 2 Record 3 > > > > Record 4 Record 5 Record 6 > > > > Record 7 Record 8 Record 9 > > > > In ASP I would use a MoveNext method and 2 For...Next loops to handle > > this. So, what is the RonR equivalent of MoveNext? > > Not sure if there is an equivalent, but something like this may work: > > # For m columns (assuming records/length is cleanly divisible by m) > <% 0.upto(@records.length/m-1) do |i| -%> > <tr> > <% 0.upto(m-1) do |j| -%> > <td><%= @records[m*i+j] %></td> > <% end %> > </tr> > <% end %> > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060117/7cb13a5a/attachment-0001.html
Whoops... typo... make sure you increment the myCount variable before the end of the @records.each loop... sorry about that... -Will On 1/17/06, Will Briggs <wrbriggs@gmail.com> wrote:> > This is ugly, but lets you change the # of columns very easily: > > <% > > myCount = 0 > numColumns = 3 > > @records.each do |record| > if myCount % numColumns == 0 > %><tr><% > end > %><td> <%= record %></td> > if myCount == numColumns > %></tr><% > end >end> %> > > > On 1/17/06, Jeremy Evans < jeremyevans0@gmail.com> wrote: > > > > On 1/17/06, Ross Malaga < malagar@mail.montclair.edu> wrote: > > > Thanks for the reply, but that does not quite work. It puts > > everything > > > in one row. But I need multiple rows and columns, like this: > > > > > > > > > Record 1 Record 2 Record 3 > > > > > > Record 4 Record 5 Record 6 > > > > > > Record 7 Record 8 Record 9 > > > > > > In ASP I would use a MoveNext method and 2 For...Next loops to handle > > > this. So, what is the RonR equivalent of MoveNext? > > > > Not sure if there is an equivalent, but something like this may work: > > > > # For m columns (assuming records/length is cleanly divisible by m) > > <% 0.upto(@records.length/m-1) do |i| -%> > > <tr> > > <% 0.upto(m-1) do |j| -%> > > <td><%= @records[m*i+j] %></td> > > <% end %> > > </tr> > > <% end %> > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060117/56fe9ec8/attachment.html
Ross Malaga wrote:> Thanks for the reply, but that does not quite work. It puts everything > in one row. But I need multiple rows and columns, like this: > > > Record 1 Record 2 Record 3 > > Record 4 Record 5 Record 6 > > Record 7 Record 8 Record 9 > > In ASP I would use a MoveNext method and 2 For...Next loops to handle > this. So, what is the RonR equivalent of MoveNext?You could use each_index, but I prefer to shift elements off lists because it''s so much more readable: <table> <% while @records.length > 0 -%> <tr> <% 3.times do -%> <td><%= @records.shift.name %></td> <% end -%> </tr> <% end -%> </table> -- Alex