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'

views/expenses/_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の代わりに@report.idを入れる必要があります

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