2010-09-01 65 views
0

我有以下要求。導軌產量 - content_for問題

例如:有一個事務表,它有列說,事務名和金額。我想遍歷事務並顯示它們的詳細信息(transaction_name和amount),最後我想在頁面的頭部(循環之前)部分顯示總金額(所有金額的總和)。 (想一想作爲概要顯示)

例頁的結構將是像

所有交易的薩姆 - 200

交易金額 TRN1 100 TRN2 50 TRN3 50

我試圖使用yield和content_for標籤,但沒有運氣。

我的代碼如下(我打電話給我的ERB文件中。)

<%= yield :transaction_summary %> 

<table> 
    <% total_amount = 0%> 
    <%for transaction in @transactions%> 
    <tr> 
     <td><%= transaction.transaction_name %></td> 
     <td><%= transaction.amount %></td> 
     <% total_amount += transaction.amount %> 
    </tr> 
    <%end%> 
    </table> 

<% content_for :transaction_summary do %> 
    <h1> 
    Sum of all the transactions - <%= total_amount %> 
    </h1> 
<% end %> 

而且

我使用一個視圖中

不是佈局內)

我正在使用rails 2.2.2

請幫助我,讓我知道是否有更好的方法

在此先感謝

歡呼

sameera

編輯:

其實我想要做的是,特定的循環,這些細節可以以後被收集之前顯示的一些細節循環

例如:如果我有一個事務對象數組,我想顯示通過和計數

感謝

回答

2

我認爲你有關於content_for和yield的錯誤想法。:) http://guides.rubyonrails.org/layouts_and_rendering.html

<h1> 
    <%= @transactions.collect(&:amount).sum -%> 
    </h1> 
    <table> 
     <%for transaction in @transactions%> 
     <tr> 
      <td><%= transaction.transaction_name %></td> 
      <td><%= transaction.amount %></td> 
     </tr> 
     <%end%> 
    </table> 

編輯 -

關於收集數據,我建議你把它們放在輔助方法:

#transactions_helper.rb 
def transactions_total transactions 
    @transactions_total ||= @transactions.collect(&:amount).sum 
end 

def passed_transactions transactions 
    @passed_transactions ||= @transactions.collect{|transaction| transaction.passed == true} 
end 

def failed_transactions transactions 
    @failed_transactions ||= transactions - passed_transactions(transactions) 
end 

只注意到對theTRON您的評論。整個乾燥原理並不適用於執行微小邏輯,例如循環遍歷數組。

+0

嗨,馬克的更加乾燥方式, 感謝您的重播。即使你的例子工作,我有一些更多的功能,如獲得通過/失敗的交易計數等。 我在想什麼是因爲我可以得到我的所有細節後循環,是否有任何軌道方式注入這些細節一個HTML DIV是上述循環 我已經更新了我的文章提的是我想做的事情 你是對的,我覺得我會錯過再次 感謝理解content_for標籤的概念,並歡迎任何幫助:d – sameera207 2010-09-01 15:53:47

+0

嗨sameera。已經更新了我的答案。 – mark 2010-09-01 16:25:21

+0

馬克喜 感謝更新 「整個乾燥的原理並不真正適用於執行微小的邏輯,如通過數組循環」 那是一個很好的點。 歡呼聲 sameera – sameera207 2010-09-03 04:06:45

0

在我看來,交易循環之前導致交易我會寫一個單獨計算總一個輔助方法,或許是沿着線的東西:

# app/helpers/transactions_helper.rb 
def calculate_total(transactions) 
total = 0 
transactions.each {|transaction| total += transaction.amount} 
total 
end 

然後你就可以在顯示它你的看法,無論你喜歡,與:

<%= calculate_total(@transactions) %> 
+0

嗨theTRON 感謝您的回覆。但我想要一種在一個循環中獲取這些細節的方法。既然你提出的方法,包括在同一個結果兩個循環分別設置 1 - helper方法 2 - 在視圖中顯示詳細信息 不知道是否有這樣做 歡呼 sameera – sameera207 2010-09-01 15:57:07

2

在其他情況下,您確實需要重用content_for,以下內容可能會有用。

在Rails 4中,可以pass :flush => true作爲一個選項content_for:

​​

在Rails 3.1.1(約)可以delete the content from the view_flow when you yield通過定義和使用以下(在application_helper例如):

def yield_and_flush!(content_key) 
    view_flow.content.delete(content_key) 
end 
+0

那''flush => true'只是讓我的一天。非常感謝。 – dkonayuki 2015-03-07 18:05:04

0

在Rails 3中,你可以在content_for之後屈服;因此您的代碼將變爲:

<% content_for :transaction_table do %> 
    <table> 
    <% total_amount = 0%> 
    <% for transaction in @transactions do %> 
     <tr> 
     <td><%= transaction.transaction_name %></td> 
     <td><%= transaction.amount %></td> 
     <% total_amount += transaction.amount %> 
     </tr> 
    <%end%> 
    </table> 

    <% content_for :transaction_summary do %> 
    <h1> 
     Sum of all the transactions - <%= total_amount %> 
    </h1> 
    <% end %> 
<% end %> <!- End content_for :transaction_table --> 


<%= yield :transaction_summary %> 
<%= yield :transaction_table %> 

注:

<% content_for :transaction_summary do %> 

不必是

<% content_for :transaction_table do %> 

內,但對於一些比較複雜的情況下,它可以。