Shub
Shub

Reputation: 265

addition in html using ruby on rails

I have a table which has price and quantity fields. I want to add the price * quantity to the grand total for each item that i eventually add to the table.

My code looks like this.

<table>
                <thead>
                    <tr>
                      <th width="200">Name</th>
            <th width="150">Price</th>
                      <th>Quantity</th>
                      <th width="150">Total</th>
                    </tr>
          <% @item.each do |item| %>
          <tr>
            <td><%= item.name %></td>
            <td><%= item.price %></td>
            <td><%= item.quantity %></td>
            <td><%= item.quantity * item.price %></td>
            <td class="actions">
              <% link_to("update", :class => 'action update') %>
              <% link_to("X", :class => 'action delete') %>
            </td>
          </tr>
          <% end %>
                </thead>
            </table>

and my grand total is in the form of a label. how do i do this? Is there static variable concept in RoR??

Upvotes: 0

Views: 153

Answers (3)

santosh
santosh

Reputation: 1611

<table>
            <thead>
                <tr>
                  <th width="200">Name</th>
        <th width="150">Price</th>
                  <th>Quantity</th>
                  <th width="150">Total</th>
                </tr>
     <% grand_total=0 %>
      <% @item.each do |item| %>
      <tr>
        <td><%= item.name %></td>
        <td><%= item.price %></td>
        <td><%= item.quantity %></td>
        <td><%= item.quantity * item.price %></td>
        <% grand_total+= item.quantity * item.price %>
        <td class="actions">
          <% link_to("update", :class => 'action update') %>
          <% link_to("X", :class => 'action delete') %>
        </td>
      </tr>
      <% end %>
            </thead>
        </table>

Upvotes: 0

vee
vee

Reputation: 38645

You could use Enumerable#inject to get the grand total as follows:

<% #= @item.inject{ |grand_total, quantity| grand_total + (quantity * cart.price) } %>

Update:

Please ignore the above line of code, that was to show you an example. The following code example should solve your issue.

Place the following line where you'd like to display the grand total.

<%= label_tag 'grand_total', 
      @item.inject(0) { |grand_total, item| grand_total + (item.quantity * item.price) } %>

@item.inject(0){ |grand_total, item| grand_total + (item.quantity * item.price) } applies the block to each item. The first parameter grand_total in this case is first assigned initial value of 0. This is done through inject(0).

The block then starts accumulating (item.quantity * item.price) into grand_total which is the final value that is returned by the inject.

Hope this makes sense.

Upvotes: 0

Kumar Akarsh
Kumar Akarsh

Reputation: 4980

You should add the field grand_total in the table and create a callback in the Item model. This callback will save the value of grand total in the table each time a new item is created.

before_save : save_grand_total

def save_grand_total
  self.grand_total = self.quantity * self.price
end

Upvotes: 2

Related Questions