簡體   English   中英

列和行中的數據,一個單元格中的一條記錄

[英]Data in columns and rows, one record in one cell

我有這個:

<% @devices.each do |device| %>
<tr>
<td>
    <% if device.photo.exists? then %>
    <%= image_tag device.photo.url(:small) %></br>
    <% end %>
    <%= link_to device.name, device %></br>
    <%= device.description %></br>
    <%= link_to 'Redaguoti', edit_device_path(device) %></br>
    <%= link_to 'Ištrinti', device, :confirm => 'Ar tikrai norite ištrinti šį prietaisą?', :method => :delete %>
</td>


<% end %>

我想要這樣的數據:

表

謝謝

您看過in_groups_of嗎? 從API 數組方法

如果我了解您的問題,這應該可以解決問題!

使用each_with_index而不是每個。 這樣,您還可以獲取索引,例如1,2,3 ..,並隨心所欲地對其進行操作。

http://www.ruby-doc.org/core/classes/Enumerable.html#M001511

您需要將每個單元格包裝在<td></td> 刪除<br>標記,它們將創建新行,而不是新單元格。

基本上,在循環內部,創建一個<tr>來創建行,然后為每個單元格創建<td>SOME DATA</td> ,然后關閉</tr>

<% @devices.each do |device| %>
<tr>
<td>
    <% if device.photo.exists? then %>
    <%= image_tag device.photo.url(:small) %></br>
    <% end %>
</td>
<td>
    <%= link_to device.name, device %>
</td>
<td>
    <%= device.description %>
</td>
<td>
    <%= link_to 'Redaguoti', edit_device_path(device) %>
</td>
<td>
    <%= link_to 'Ištrinti', device, :confirm => 'Ar tikrai norite ištrinti šį prietaisą?', :method => :delete %>
</td>
</tr>

<% end %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM