在帐户索引中,我想显示我在表格中所做的帐户信息,以及手风琴或者我的购买信息。
我的帐户模型是
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
端
在 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>
我想扩展行点击account.purchases,但我没有做对。