Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write a while loop in erb

I have a table with two columns level, and slot. each level can have many slots, but I have made one table that models this case. example

level    |   slot
1        |   1
1        |   2
1        |   2
2        |   1
2        |   2
2        |   2

and in my erb template I want to make a table that will model this case by making something like a grid. The issue so far, is I want somethink like two loops to handle this situation. for each level, I get the number of slots and display them. the provided each loop is not helping. I am thinking of creating a variable that will keep track of the level variable, and when it changes, I call the inner loop. but this does not loop efficient to me.

I am thinking also of creating a json object and make the slots apeats as an array inside the level variable, but I have no idea from where to start this.

or is there a way to write a while loop in the erb template?

like image 421
anyavacy Avatar asked Sep 02 '25 02:09

anyavacy


2 Answers

You can write a loop in ruby (erb) this way:

<% loop do %>
  ...
  <% break if <condition> %>
<% end %>

See Is there a "do ... while" loop in Ruby?

like image 148
mgrim Avatar answered Sep 04 '25 18:09

mgrim


ERB is just ruby

<% @collection.each do |value| %>
  <%= value %>
<% end %>
like image 28
CuriousMind Avatar answered Sep 04 '25 19:09

CuriousMind