【问题标题】:ActionController::RoutingError in Bank_accounts#indexBank_accounts#index 中的 ActionController::RoutingError
【发布时间】:2012-03-14 23:18:20
【问题描述】:

每次我尝试访问“银行账户”的索引时,我似乎都会在我的 Rails 项目中遇到路由错误。我对 Rails 很陌生,所以这应该是一个简单的错误。如果我遗漏了什么,我深表歉意。

我遇到的错误:

ActionController::RoutingError in Bank_accounts#index 显示第 21 行出现的 app/views/bank_accounts/index.html.erb

这是 Rails 似乎不喜欢的那行代码:

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

路由文件:

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

银行账户的索引视图

<% @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>

【问题讨论】:

  • 你能发布rake routes(或bundle exec rake routes)的输出吗?
  • 嗨 Scott,我是 Rails 新手,我想这是通过 Rails 控制台运行的?我尝试通过 Rails 控制台运行它,但我得到的只是“NameError: undefined local variable or method `routes' for #<0x1b576f8>

标签: ruby-on-rails ruby


【解决方案1】:

我对 Rails 命名约定的理解是,如果要链接到嵌套资源的索引,则需要使用复数:

bank_account_transactions_path(bank_account)

如果您想链接到特定的嵌套资源,请使用单数并将嵌套资源 ID 作为第二个参数传递:

bank_account_transaction_path(bank_account, txnid)

this question 有一个更简洁的语法示例 - 你可能更喜欢它。

希望有帮助!

【讨论】:

    【解决方案2】:

    你应该 资源:bank_accounts 做 成员“交易”,:方法=>:获取 结束

    在您的索引中 交易银行账户路径(账户)

    控制器 默认交易 …… 结束

    【讨论】:

      猜你喜欢
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-07
      相关资源
      最近更新 更多