1

In the accounts index, I would like to display the account information which im doing in a table, and in an accordion or dropwon my purchases information.

My Account Model is

class Account < ActiveRecord::Base
#Associations
has_many :purchases, dependent: :destroy  do
  def total
      total = 0
      self.each { |purchase| total += purchase.total }
      total
  end
end

has_many :sales, dependent: :destroy

has_many :payments, dependent: :destroy

#Validations
validates :name, presence: true
validates :category, presence: true

end

Which shows in its index.html.erb

<div class="row table-responsive" style="margin-top:50px;">
<table id="accountstable"class="table table-striped custab tablesorter">
  <thead>
  <tr style="margin-bottom:20px;">
    <%= link_to 'Add Account', new_account_path ,:class => "btn btn-primary btn-xs pull-center"%>
  </tr>
    <tr>
     <th>Name</th>
     <th>Category</th>
     <th>Balance</th>
     <th>Transactions</th>
   </tr>
 </thead>
 <% @accounts.each do |account| %>

 <tr>
  <td><%= account.name %></td>
  <td><%= account.category %></td>
  <td><%= 0 +  account.purchases.total %></td>

  <td><%= link_to 'Edit', edit_account_path(account), :class=>"btn btn-info btn-xs" %></td>
  <td><%= link_to 'Delete', account, method: :delete, data: { confirm: 'Sure ?' },:class=>"btn btn-danger btn-xs" %>
    <% end %>
  </table>
</div>

I would like to expand on row click account.purchases, but im not getting it right.

alex
  • 4,689
  • 8
  • 43
  • 83

0 Answers0