2

I seem to be getting a routing error within my Rails project each time I try access the index for "Bank Accounts". I'm quite new to Rails so this should probably be a simple error. If I've missed anything I apologise.

Errors I'm getting:

ActionController::RoutingError in Bank_accounts#index Showing app/views/bank_accounts/index.html.erb where line #21 raised

This is the line of code that Rails doesn't seem to like:

<td><%= link_to 'Transaction Details', bank_account_transaction_path(bank_account) %>  </td>

Routes file:

ActionController::Routing::Routes.draw do |map|
map.resources :bank_accounts, :has_many => [:transactions]

map.root :controller => "bank_accounts"

map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end

Index view for Bank Accounts

<% @bank_accounts.each do |bank_account| %>
<tr>
<td><%=h bank_account.account_number %></td>
<td><%=h bank_account.holders_name %></td>
<td><%=h bank_account.overdraft_limit %></td>
<td><%=h bank_account.current_balance %></td>
<td><%=h bank_account.active %></td>
<td><%= link_to 'Show', bank_account %></td>
<td><%= link_to 'Edit', edit_bank_account_path(bank_account) %></td>
<td><%= link_to 'Transaction Details', bank_account_transaction_path(bank_account) %>      </td>
<td><%= link_to 'Destroy', bank_account, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
  • Can you post the output of `rake routes` (or `bundle exec rake routes`)? – ScottJShea Mar 14 '12 at 23:21
  • Hi Scott, I'm new to Rails, I presume this is run through the Rails console? I've tried running it via the Rails console, but all I get is "NameError: undefined local variable or method `routes' for #" - so I presume I was wrong... –  Mar 14 '12 at 23:26
  • You would want to do it from the command line in the project folder for your app (same place the Gemfile is). I am sorry I missed this but Xavier may have hit on the issue in his answer below. – ScottJShea Mar 14 '12 at 23:31
  • I have it working with Xavier's solution. Thanks for the help. –  Mar 14 '12 at 23:35

2 Answers2

1

My understanding of the Rails naming convention is that if you want to link to an index of nested resources, you need to use the plural:

bank_account_transactions_path(bank_account)

And if you want to link to a particular nested resource, use the singular and pass in the nested resource ID as a second argument:

bank_account_transaction_path(bank_account, txnid)

And this question has an example of a cleaner syntax - you might like that better.

hope that helps!

Community
  • 1
  • 1
Xavier Holt
  • 14,471
  • 4
  • 43
  • 56
0

You should Resources :bank_accounts do member 'transaction', :method=> :get End

In your index transaction_bank_account_path(account)

Controller Def transaction .... End