列和行中的数据,一个单元格中的一个记录

时间:2011-04-19 17:01:46

标签: ruby-on-rails ruby-on-rails-3

我有这个:

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

我想获得这样的数据:

table

谢谢

3 个答案:

答案 0 :(得分:1)

你看过in_groups_of了吗?来自API Array Methods

如果我理解你的问题,这应该可以解决问题!

答案 1 :(得分:0)

使用each_with_index而不是每个。这样,你也可以获得索引,比如1,2,3 ..并随意做任何事情。

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

答案 2 :(得分:0)

您需要将每个单元格包装在<td>&amp; </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 %>