2016-08-22 3 views
0

Я новичок в Rails, и я пытаюсь создать приятное приложение, и я борюсь с массивами, у меня есть 4 массива, которые я хочу повторить, и они не имеют одинакового размера.Iterate 4 Arrays with Rails

Я хочу создать разделы в HTML с использованием массива первого, что я сделал это

@sections = ['Section One','Section Two','Section Three','Section Four'] 
@itemsOne = ['item 1','item 2','item 3','item 4','item 5','item 6'] 
@itemsTwo = ['item 1','item 2','item 3','item 4','item 5','item 6'] 

Я использовал

<%= @sections.zip(@itemsOne, @itemsTwo).each do |t1, t2, t3| %> 
<%= t1 %> 
<table> 
    <tbody> 
     <tr> 
      <td> 
       <%= t2 %> | <%= t3 %> 
      </td> 
      <td> 
       <%= t2 %> | <%= t3 %> 
      </td> 
      <td> 
       <%= t2 %> | <%= t3 %> 
      </td> 
     </tr> 
    </tbody> 
</table> 
<% end %> 

у меня есть таблица, в которой имеет названия раздела и клетки, которые имеют два значения

, но то, что я получаю значение |t2| в каждой ячейке |t1| секции используя @Phil ответ вниз ниже, но он удалил его.

<%= @sections.zip(@itemsOne, @itemsTwo).each do |t| %> 
<%= t[0] %> 
<table> 
    <tbody> 
     <tr> 
      <td> 
       <%= t[1] %> | <%= t[2] %> 
      </td> 
      <td> 
       <%= t[1] %> | <%= t[2] %> 
      </td> 
      <td> 
       <%= t[1] %> | <%= t[2] %> 
      </td> 
     </tr> 
    </tbody> 
</table> 
<% end %> 

p.s. itemsOne и itemsTwo массивы имеют более 20 значений.

ответ

0

То, что я создал, разделяет мои большие массивы на более мелкие, а затем перебирается через каждый этот путь без таблицы, потому что в таблице возникают проблемы с дизайном, поэтому я пошел в div, используя колонку bootstrap 3, может быть лучший способ, но это что я получил как новичок.

<div class="row"> 
<div class="col-md-12"> 

<h4><%= @Sections[0] %></h4> 
<!-- This will Display Section 0 in the Array --> 
</div> 

<div class="row"> 
    <div class="col-md-12"> 

    <% @count = 0 %> 
    <!-- Counter is Zero --> 
    <% @ItemsOne.collect do |t1| %> 
    <!-- This will loop array to increment the @count and repeat the HTML --> 
    <% @count += 1 %> 
    <!-- With each loop increment by 1--> 

    <div class="col-md-3"> 
     <div class="col-md-12"> 
     <label> 
     <input type="checkbox" name="optionsCheckboxes"> 
     <%= @ItemsOne[@count - 1] %> 
     <!-- Counter should start from 0 adding -1 will make it start 
     from 0 instead of 1 and then will print the value of the Index Number --> 
     </label> 
     </div> 
    </div> 

    <% end %> 

    </div> 
    </div> 
</div> 
0

Вот еще один способ сделать это

<div class="row"> 
    <% @Sections.each_with_index do |x1, n| %> 
<div class="row"> 
    <div class="col-md-12"> 
     <h4><%= @Sections[n] %></h4> 
    </div> 

<div class="row"> 
    <div class="col-md-12"> 
    <% if n == 0 %> 
    <% @itemsOne.each_with_index do |t1, n| %> 
    <div class="col-md-3"> 
     <div class="col-md-12"> 
     <label> 
      <input type="checkbox" name="optionsCheckboxes"> 
      <%= @itemsOne[n] %> 
     </label> 
     </div> 
    </div> 
    <% end %> 
<% elsif n == 1 %> 
    <% @itemsTwo.each_with_index do |t1, n| %> 
    <div class="col-md-3"> 
     <div class="col-md-12"> 
      <label> 
      <input type="checkbox" name="optionsCheckboxes"> 
       <%= @itemsTwo[n] %> 
      </label> 
     </div> 
    </div> 
    <% end %> 

<% elsif n == 2 %> 
    <% @itemsThree.each_with_index do |t1, n| %> 
    <div class="col-md-3"> 
     <div class="col-md-12"> 
      <label> 
      <input type="checkbox" name="optionsCheckboxes"> 
       <%= @itemsThree[n] %> 
      </label> 
     </div> 
    </div> 
<% end %> 

<% elsif n == 3 %> 
    <% @itemsFour.each_with_index do |t1, n| %> 
    <div class="col-md-3"> 
     <div class="col-md-12"> 
      <label> 
      <input type="checkbox" name="optionsCheckboxes"> 
       <%= @itemsFour[n] %> 
      </label> 
     </div> 
    </div> 
<% end %> 

<% end %> 
</div> 
</div> 
</div> 
<% end %> 

</div>