0

我正在启动一个新的 Rails 3.2.6 应用程序。当我尝试查看新的嵌套表单时,我的嵌套路由失败。我将首先显示错误,然后显示所有涉及的代码。

尝试访问的 URL:http://localhost:3000/reports/1/expenses/new

No route matches {:action=>"show", :controller=>"expenses", :report_id=>#<Report id: 1, name: "Test Report", created_at: "2012-06-07 21:58:37", updated_at: "2012-06-07 21:58:37">}

路线.rb

resources :reports do
  resources :expenses
end

费用_控制器.rb

def new
  @report = Report.find(params[:report_id])
  @expense = @report.expenses.new
end

意见/费用/new.html.haml

%h1 New Expense
= render 'form'

意见/费用/_form.html.haml

= form_for [@report, @expense] do |f|

这是我试图点击的链接:

= link_to 'New Expense', new_report_expense_path(@report)

当我明确调用新操作时,我无法弄清楚为什么它试图访问 show 操作。

耙子路线

report_expenses     GET    /reports/:report_id/expenses(.:format)          expenses#index
                    POST   /reports/:report_id/expenses(.:format)          expenses#create
new_report_expense  GET    /reports/:report_id/expenses/new(.:format)      expenses#new
edit_report_expense GET    /reports/:report_id/expenses/:id/edit(.:format) expenses#edit
report_expense      GET    /reports/:report_id/expenses/:id(.:format)      expenses#show
                    PUT    /reports/:report_id/expenses/:id(.:format)      expenses#update
                    DELETE /reports/:report_id/expenses/:id(.:format)      expenses#destroy
        reports     GET    /reports(.:format)                              reports#index
                    POST   /reports(.:format)                              reports#create
     new_report     GET    /reports/new(.:format)                          reports#new
    edit_report     GET    /reports/:id/edit(.:format)                     reports#edit
         report     GET    /reports/:id(.:format)                          reports#show
                    PUT    /reports/:id(.:format)                          reports#update
                    DELETE /reports/:id(.:format)                          reports#destroy
           root            /                                               reports#index

更新 GitHub 存储库链接:https ://github.com/ardavis/expense_report

4

3 回答 3

4

错误在views/expences/_form.html.haml,最后一行

      = link_to 'Cancel', report_expense_path(@report), class: 'btn'

你可能是说

      = link_to 'Cancel', report_path(@report), class: 'btn'
于 2012-06-16T08:41:02.440 回答
0

您是否尝试过路线:

map.resources :reports do |report|
    report.resources :expenses
end
于 2012-06-16T05:34:02.917 回答
-1

您需要在链接中添加 @report.id 而不是 @report

link_to 'New Expense', new_report_expense_path(@report.id)
于 2012-06-16T04:54:15.590 回答