2016-08-22 84 views
0

我是Rails的新手,我正在嘗試構建一個不錯的應用程序,我正在努力使用數組,我有4個數組,我想要迭代並且它們的大小不一樣用Rails迭代4陣列

我想用第一陣列我做到了

@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'] 

生成HTML格式的部分我是用

<%= @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|在使用@Phil答案樓下|t1|部分 每個單元格的值,但他刪除了它。

<%= @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的使用引導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>