How to write a while loop in erb
I have a table with 2 column levels and a slot. each level can have many slots, but I created one table that simulates 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 create a table that will simulate this case by doing something like a grid. The problem so far, I want something like two loops to handle this situation. for each level, I get the number of slots and display them. the provided loop each
doesn't help. 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 it doesn't work for me.
I also want to create a json object and make the slots imitators as an array inside a level variable, but I don't know where to start.
or is there a way to write a while loop in an erb pattern?
source to share
It can still be one table. I would create a variable called @slots
in your action and call .each
it in your view erb like this:
<% @slots.each do |slot| %>
<% if ... %>
<div class="left-column">
<%= slot.level %>
</div>
<div class="right-column">
<%= slot.id %> <!-- or whatever var you you want -->
</div>
<% else %>
...
<% end %>
<% end %>
This way every new one slot
will be automatically added to the collection and you don't have to worry about changes tolevel
source to share