2016-08-22 3 views
0

Ich bin neu in Rails und ich versuche, eine nette Anwendung zu bauen, und ich habe mit Arrays zu kämpfen, ich habe 4 Arrays, die ich iterieren will und sie haben nicht die gleiche GrößeIterieren 4 Arrays mit Rails

ich möchte Abschnitte in HTML zu erzeugen, um das erste Array, was ich es

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

ich war

<%= @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 %> 

ich eine Tabelle verwenden, die ein Abschnitt Titel haben und Zellen, die zwei Werte

haben, aber was ich bekommen, ist der Wert von |t2| in jeder Zelle |t1| Abschnitt mit @Phil Antwort unten, aber er es gelöscht.

<%= @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. Die ItemsOne und Items Zwei Arrays haben mehr als 20 Werte.

Antwort

0

Was ich erstellt habe, ist meine großen Arrays in kleinere getrennt und dann durch jede dieser Art ohne eine Tabelle durchlaufen, weil Tabelle Designprobleme machte, so ging ich div mit Bootstrap 3 Spalte, könnte es einen besseren Weg, aber das ist was ich als Anfänger bekommen habe.

<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

Hier ist eine weitere Möglichkeit, dieses

<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> 
zu tun