2013-03-06 60 views
0

相關片段合併表到單個表

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中獲得最終結果,將會容易得多。如果不可能,請給我最好的方式?

+0

在你的 'show.html.erb' 的代碼重複兩次。那是故意的嗎? – 2013-03-06 14:18:55

+0

每個outbound_message都具有相同的一組密鑰嗎? – 2013-03-06 14:20:43

+0

@Charles - 是的,每個outbound_message都有相同的一組密鑰。點擊圖片鏈接獲取樣本結果。 – 2013-03-06 14:21:37

回答

1

如果你不想渲染一個單獨的表爲每個對象,怎麼樣像這樣在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具有相同的一組密鑰的情況下才有效。

+0

完全是我的想法,但這也需要在show.rb中進行更改。不過,這是一個很好的解決方案。 – 2013-03-06 14:46:46

+0

您將不得不更改show.html.erb以實現所需內容,這是outbound_messages集合中每個對象的表中的新單行。 – 2013-03-06 14:53:16

+0

但是,如果要將對show.html.erb的影響降到最低,可以將整個outbound_messages集合傳遞給partial,而不僅僅是一條記錄。 – 2013-03-06 14:55:57

0

在這裏,你去..

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

您可以消除部分完全

+0

幾天前我最初的做法是這樣的,但是有太多的鍵可以像這樣輸入。我有出站消息,訂單,預訂等,因此這種方法不起作用。 – 2013-03-06 14:45:00