合并表到单个表
相关片段合并表到单个表
Show.html.erb
<% outbound_messages.each do |outbound_message| %>
<h5>Outbound Message</h5>
<%= render "trace/display_tabular_data", :data => outbound_message %>
<% end %>
Display_tabular_data.html.erb
<table border="1px solid black">
<thead>
<tr>
<%data.each do |key,value|%>
<th><%=key.capitalize%></th>
<%end%>
</tr></thead><tr>
<%data.each do |key,value|%>
<td><%=value%></td>
<%end%>
</tr>
</table>
那么,什么情况是,每行数据,获取打印在一张独特的桌子上。 所以一个个有像http://imgur.com/1gskRvX
但显然一个更好的结果将是作为一个单一的表(预期结果)
Outbound Message
Message ID, Exchange, Relayed
Row1
Row2
Row3
Row4
...
....
任何想法我怎么能去呢? Display_tabular_data在show.html.erb中的不同位置被调用至少15次,所以如果通过在display_tabular_data中进行更改,而不是在show.html.erb中获得最终结果,将会容易得多。如果不可能,请给我最好的方式?
如果你不想渲染一个单独的表为每个对象,怎么样像这样在show.html.erb:
<% unless outbound_messages.empty? %>
<%= render 'trace/display_tabular_data', :data => outbound_messages %>
<% end %>
然后在部分:
<h5>Outbound Messages</h5>
<table border="1px solid black">
<thead>
<tr>
<% data.first.each do |key,value| %>
<th><%=key.capitalize%></th>
<% end %>
</tr>
</thead>
<% data.each do |outbound_message| %>
<tr>
<% outbound_message.each do |key,value|%>
<td><%=value%></td>
<% end %>
</tr>
</table>
这只有在你确信每个outbound_message具有相同的一组密钥的情况下才有效。
完全是我的想法,但这也需要在show.rb中进行更改。不过,这是一个很好的解决方案。 – 2013-03-06 14:46:46
您将不得不更改show.html.erb以实现所需内容,这是outbound_messages集合中每个对象的表中的新单行。 – 2013-03-06 14:53:16
但是,如果要将对show.html.erb的影响降到最低,可以将整个outbound_messages集合传递给partial,而不仅仅是一条记录。 – 2013-03-06 14:55:57
在这里,你去..
<% if outbound_messages.count > 0 %>
<h5>Outbound Message</h5>
<table border="1px solid black">
<thead>
<tr>
<td>Message ID</td>
<td>Exchange</td>
<td>Relayed</td>
</tr>
</thead>
<% outbound_messages.each do |outbound_message| %>
<tr>
<td>
<%= outbound_message[:message_id] %>
</td>
<td>
<%= outbound_message[:exchange] %>
</td>
<td>
<%= outbound_message[:relayed] %>
</td>
</tr>
<% end %>
</table>
<% end %>
您可以消除部分完全
几天前我最初的做法是这样的,但是有太多的键可以像这样输入。我有出站消息,订单,预订等,因此这种方法不起作用。 – 2013-03-06 14:45:00
在你的 'show.html.erb' 的代码重复两次。那是故意的吗? – 2013-03-06 14:18:55
每个outbound_message都具有相同的一组密钥吗? – 2013-03-06 14:20:43
@Charles - 是的,每个outbound_message都有相同的一组密钥。点击图片链接获取样本结果。 – 2013-03-06 14:21:37