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
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
header |
@record.each do |r|
<%=r.name%> |
etc....
<%end%>
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
header |
@record.each do |r|
<%=r.name%> |
etc....
<%end%>
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
Something like this would work with just one loop. Making it
prettier is left as an exercise for the reader.
-Rob
<% records = @Record.find(:all)
per_row = 3
records.each_index { |position|
if position.modulo(per_row).zero? -%>
<% end -%>
Record <%= "#{position.next}: #{records[position].name}" %> |
<% if position.next.modulo(per_row).zero? -%>
<% end
}
if ! records.length.modulo(per_row).zero? -%>
<% end -%>
This is ugly, but lets you change the # of columns very easily:
<%
myCount = 0
numColumns = 3
@records.each do |record|
if myCount % numColumns == 0
%>
<%
end
%> <%= record %> |
if myCount == numColumns
%>
<%
end
end
%>
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 B. [email protected] wrote:
%><tr><%
end
%> <%= record %>
if myCount == numColumns
%><%
end
end
On 1/17/06, Ross M. [email protected] wrote:
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| -%>
<% 0.upto(m-1) do |j| -%>
<%= @records[m*i+j] %>
<% end %>
<% end %>
Ross M. wrote:
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:
<% while @records.length > 0 -%>
<% 3.times do -%>
<%= @records.shift.name %> |
<% end -%>
<% end -%>