2012-06-16 19 views
0

我開始一個新的Rails 3.2.6應用程序。當我嘗試查看新的嵌套表單時,我的嵌套路由失敗。我會先顯示錯誤,然後顯示所有涉及的代碼。Rails新動作嘗試顯示

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">} 

的routes.rb

resources :reports do 
    resources :expenses 
end 

expenses_controller.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 

UPDATE 鏈接到GitHub庫:https://github.com/ardavis/expense_report

回答

4

的錯誤是在views /筆開支/ _form.html.haml,最後一行

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

你可能是指

 = link_to 'Cancel', report_path(@report), class: 'btn' 
+1

這是Rails堆棧跟蹤比看起來更有用的一個很好的例子。在錯誤信息附近,會出現一個文件名和行號,它會指向你的這一行,而不是你認爲錯誤的那一行。 – Gareth

+0

哇,非常感謝。你在哪裏看到堆棧跟蹤?我看到的唯一錯誤是「路由錯誤」,在其下面沒有任何內容來查看跟蹤。隨意克隆回購,並嘗試自己。我不知道我應該用什麼方法來追蹤它。 – ardavis

+1

有了這樣的錯誤,Rails不會在瀏覽器中顯示跟蹤,只能在服務器控制檯中顯示。不知道爲什麼。 – dimuch

-1

你需要把@ report.id而不是@report在您的鏈接

link_to 'New Expense', new_report_expense_path(@report.id) 
+0

雖然我希望這是解決方案,不是這樣。我之前做過這件事,而且我知道這不是必需的。我試過你的解決方案,它給出了完全相同的錯誤,沒有任何區別。 – ardavis

+0

織補。對於那個很抱歉。我只是不明白爲什麼整個對象都試圖通過id。但我想這是正常的,軌道從它挑選的ID。 – Abram

+0

你有沒有嘗試添加一個自定義路由接受:id作爲參數?也許匹配「/費用/新/:ID」=>「費用#新」 – Abram

0

您是否嘗試過路線:

map.resources :reports do |report| 
    report.resources :expenses 
end 
+0

Ah nm模式中覆蓋這個方法,那也是錯誤的。你已經做得很好,據我所知。 – Abram

相關問題