I'm trying to create a two dimensional array like this...
@transactions = {}
@expenses.each do |expense|
@transactions[expense.id.to_s + '-expense'] = {'identifier' => expense.id.to_s + '-expense', 'id' => expense.id, 'type' => 'expense', 'issued_on' => expense.issued_on, 'amount' => expense.amount}
end
@invoices.each do |invoice|
@transactions[invoice.id.to_s + '-invoice'] = {'identifier' => invoice.id.to_s + '-invoice', 'id' => invoice.id, 'type' => 'invoice', 'issued_on' => invoice.issued_on, 'amount' => invoice.total}
end
But when I iterate over the array to create rows, it gives me the key first, then the whole array like this...
- 1-expense
- identifier: 1-expense
id: 1
type: expense
issued_on: 2012-05-03
amount: 8.99
---
- 2-expense
- identifier: 2-expense
id: 2
type: expense
issued_on: 2012-05-25
amount: 4.96
---
- 3-expense
- identifier: 3-expense
id: 3
type: expense
issued_on: 2012-05-01
amount: 56.08
I iterate to create rows (of course, im supposed to do something like transaction.id or transaction.issued_on but i debug so you can see what i mean)...
<% @transactions.each do |transaction| %>
<tr class="<%= cycle("odd", "even") %>">
<td>
<%= debug transaction %>
</td>
<td></td>
<td></td>
<td></td>
</tr>
<% end %>
so I end up with a table like this...
| 1-expense |
| identifier: 1-expense, id: 1, type: expense, issued_on: 2012-05-03, amount: 8.99 |
| 2-expense |
| identifier: 2-expense, id: 2, type: expense, issued_on: 2012-05-25, amount: 4.96 |
| 3-expense |
| identifier: 3-expense, id: 3, type: expense, issued_on: 2012-05-01, amount: 56.08 |
when my table is supposed to look like this
| identifier: 1-expense, id: 1, type: expense, issued_on: 2012-05-03, amount: 8.99 |
| identifier: 2-expense, id: 2, type: expense, issued_on: 2012-05-25, amount: 4.96 |
| identifier: 3-expense, id: 3, type: expense, issued_on: 2012-05-01, amount: 56.08 |
I don't know how to just create a simple table with transaction data rows and not those extra rows with the hash key.
You've created a hash with the {}
syntax, not an array.
If you want to iterate over just the values of the hash (without the keys), you can do this:
<% @transactions.each_value do |transaction| %>
...
Or, you could use an array to hold the rows:
@transactions = []
@expenses.each do |expense|
@transactions << {'identifier' => expense.id.to_s + '-expense', 'id' => expense.id, 'type' => 'expense', 'issued_on' => expense.issued_on, 'amount' => expense.amount}
end
@invoices.each do |invoice|
@transactions << {'identifier' => invoice.id.to_s + '-invoice', 'id' => invoice.id, 'type' => 'invoice', 'issued_on' => invoice.issued_on, 'amount' => invoice.total}
end
And then your existing erb template should work better.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.