I’m attempting to learn Ruby on Rails by creating a project and I can’t seem to get my head around an error I keep getting. If I don’t get the “NoMethodError”, I’ll get something like “You have a nil object when you didn’t expect it!” – I simply want to view a bank account and have it show the transactions on that bank account. My problem may be with the controller, but I’ve tried all sorts of different things and can’t figure it out. Much help would be greatly appreciated. I think I’ve included everything, if not, I apologise and will include what ever else is needed.
Bank Account Controller Show (I think the error is in the show):
def show
@bank_account = BankAccount.find(params[:id])
@transactions = @bank_account.transaction
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @bank_account }
end
Views: bank_accounts>show.html.erb
<% @transactions.each do |transaction| %>
<tr>
<td><%=h transaction.transaction_id %></td>
<td><%=h transaction.dateD %></td>
<td><%=h transaction.trans_type %></td>
<td><%=h transaction.amount %></td>
<td><%=h transaction.new_balance %></td>
<td><%=h transaction.transaction_success %></td>
<td><%= link_to 'Show', transaction %></td>
<td><%= link_to 'Edit', edit_transaction_path(transaction) %></td>
<td><%= link_to 'Destroy', transaction, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
I think this is the backtrace (sorry for not including it).
“BankAccount/app/controllers/bank_accounts_controller.rb:17:in `show'”
Also, if I do the following for the bank account controller:
def show
@bank_account = BankAccount.find(params[:id])
@transactions = @bank_account.transactions
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @bank_account }
end
I get this SQL error:
“ActiveRecord::JDBCError: [SQLITE_ERROR] SQL error or missing database (no such column: transactions.bank_account_id): SELECT * FROM “transactions” WHERE (“transactions”.bank_account_id = 1)”
if your BankAccount model has many transactions, the method to access them is plural: