Frage

Dies ist wahrscheinlich sehr einfach, aber ich habe eine harte Zeit, es herauszufinden.

Ich habe einen Teil:

<% for room in @scrape %>
<tr id="page_<%= room.id %>">
    <th scope="row" class="<%= cycle("spec", "specalt") -%>"><%=h room.name %></td>
    <td class="<%=current_cycle%>"><%=h room.day1 %></td>
    <td class="<%=current_cycle%>"><%=h room.day2 %></td>
    <td class="<%=current_cycle%>"><%=h room.day3 %></td>
    <td class="<%=current_cycle%>"><%=h room.day4 %></td>
    <td class="<%=current_cycle%>"><%=h room.day5 %></td>
    <td class="<%=current_cycle%>"><%=h room.day6 %></td>
    <td class="<%=current_cycle%>"><%=h room.day7 %></td>
    <td class="<%=current_cycle%>"><%= select_tag("room[#{room.id}]", options_for_select(0..room.spots,0)) %></td>

</tr>
<% end %>

Aus find_by_sql Ergebnis wie folgt:

    ID         Room     Day1     Day2   Day3    Day4    Day5    Day6    Day7
   18298   Blue Room   13.23    13.23   13.23   13.23   13.23   13.23   13.23

Aber ich weiß nicht, wie viele Tage es wird, wie kann ich Schleife durch die Spalte Ergebnisse für die verschiedenen Tage?

War es hilfreich?

Lösung

Dies könnte in einem Helfer mit Block / Ausbeute durchgeführt werden, aber das ist nicht in dem Anwendungsbereich Ihrer Frage. Ich bin gleich auf die Frage erhalten, indem diese in dem Teil tun.

<% room.attributes.each do |key, value| %>
  <% if key.to_s.include?("day") %>
    <td class="<%=current_cycle%>"><%=h value.to_s %></td>
  <% end %>
<% end %>

Update: Hier ist der Helfer Beispiel. Wenn dieses Muster in Ihrer Anwendung mehr als einmal auftauchen wird, ich denke, das ist sowohl besser verwaltbar und lesbar.

def attributes_for(model, match, &block)
  model.attributes.each do |key, value|
    if key.to_s.include?(match)
      # we pass key and value in this example. but you can
      # pass whatever you want to the block.
      concat(capture(key, value, &block))
    end
  end
end 

Das ist jetzt Ihr Teil:

<% attributes_for(room, "day") do |key, value| %>
  <td class="<%=current_cycle%>"><%=h value.to_s %></td>
<% end %>

Weitere Gesamtcodezeilen, aber besser, wenn Sie in Ihrer App tun dies gehen werden.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top