Note that there are some explanatory texts on larger screens.

plurals
  1. PORails yield - content_for problem
    text
    copied!<p>I have the following requirement. </p> <p>Ex: There is a transaction table where it has columns say, transaction_name and amount. I want to loop through the transactions and display their details (transaction_name and amount) and finally I want to display the total amount (sum of all the amounts) in the head (before the loop) section of my page. (Think about it as a summary display)</p> <p>Example page structure would be like</p> <p><strong>Sum of all the transactions - 200</strong></p> <p>transaction amount trn1 100 trn2 50 trn3 50</p> <p>And I tried to use yield and content_for tag but no luck. </p> <p>my code is as follows (i'm calling inside my erb file.)</p> <pre><code>&lt;%= yield :transaction_summary %&gt; &lt;table&gt; &lt;% total_amount = 0%&gt; &lt;%for transaction in @transactions%&gt; &lt;tr&gt; &lt;td&gt;&lt;%= transaction.transaction_name %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= transaction.amount %&gt;&lt;/td&gt; &lt;% total_amount += transaction.amount %&gt; &lt;/tr&gt; &lt;%end%&gt; &lt;/table&gt; &lt;% content_for :transaction_summary do %&gt; &lt;h1&gt; Sum of all the transactions - &lt;%= total_amount %&gt; &lt;/h1&gt; &lt;% end %&gt; </code></pre> <p>And </p> <p>I'm using with inside a view (<strong>not inside a layout</strong>) </p> <p>I'm using rails 2.2.2</p> <p>Please help me and let me know if there is a better way</p> <p>thanks in advance</p> <p>cheers</p> <p>sameera</p> <p>EDIT: </p> <p><strong>Actually what I want to do is , Display some details before a particular loop where those details can be collected after the loop</strong></p> <p>Ex: If i have an array of transaction objects, I want to show a count of pass and failed transactions before the transactions loop in my view</p> <p>thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload