Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1049269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:35:19+00:00 2026-05-16T16:35:19+00:00

I have the following requirement. Ex: There is a transaction table where it has

  • 0

I have the following requirement.

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)

Example page structure would be like

Sum of all the transactions – 200

transaction amount
trn1 100
trn2 50
trn3 50

And I tried to use yield and content_for tag but no luck.

my code is as follows (i’m calling inside my erb file.)

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

And

I’m using with inside a view (not inside a layout)

I’m using rails 2.2.2

Please help me and let me know if there is a better way

thanks in advance

cheers

sameera

EDIT:

Actually what I want to do is , Display some details before a particular loop where those details can be collected after the loop

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

thanks

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T16:35:20+00:00Added an answer on May 16, 2026 at 4:35 pm

    I think you have the wrong idea about content_for and 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>
    

    edit –

    Regarding collecting data, I suggest you put them in helper methods:

    #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
    

    Just noticed your comment to theTRON. The whole dry principle doesn’t really apply to executing tiny logic such as looping through a array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following requirement where there are 3 scripts say, a2,a3,a4 Now as
In a web project using jsp, I have following requirement Upload a file (say
I have the following requirement. I have a table with a column that contains
I have following requirement, std::vector< std::vector < std::string > > segments; There are unknown
Hi I have the following requirement. I want to create variable number of String
I have the following requirement: - I have many (say 1 million) values (names).
I have a following requirement for a very complex UI. (Complex here means there
I have following requirement, I have C#/.Net console application, which refers to 'System.Data.Sqlite.dll' 'System.Data.Sqlite.dll'
I have following requirement in my application. A tableView with games title After selecting
I am developing a network protocol In which I have following requirement each node

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.