2012-08-02 97 views
0

當我嘗試通過單擊編輯鏈接(如下所述)從其顯示頁面編輯子資源(課程)時,我遇到了嵌套路由問題。有兩個類別:單位和課程。我不明白爲什麼Rails試圖路由到課程管理員(單位的母親)。Rails路由到祖父母嵌套類

任何幫助將不勝感激。

我收到此錯誤和URL

http://localhost:3000/units/3/lessons/13/edit 
Routing Error 

No route matches {:action=>"show", :controller=>"courses", :id=>nil} 

的config/routes.rb中 資源:課程做 資源:單位 結束

resources :units do 
    resources :lessons 
end 

**從耙路線**

對於點擊鏈接

<%= link_to "Edit Lesson", edit_unit_lesson_path(@unit, @lesson) %> 

LessonsController

代碼

class LessonsController < ApplicationController 
    before_filter :find_unit 
    before_filter :find_lesson, :only => [:show,:edit,:update,:destroy] 
    . 
    . 
    . 
private 
def find_unit 
    @unit = Unit.find(params[:unit_id]) 
end 

def find_lesson 
    @lesson = @unit.lessons.find(params[:id]) 
end 

服務器日誌文件

Started GET "/units/3/lessons/12/edit" for 127.0.0.1 at 2012-08-02 14:50:55 +0200 
Processing by LessonsController#edit as HTML 
    Parameters: {"unit_id"=>"3", "id"=>"12"} 
    Unit Load (0.1ms) SELECT "units".* FROM "units" WHERE "units"."id" = ? LIMIT 1 [["id", "3"]] 
Lesson Load (0.1ms) SELECT "lessons".* FROM "lessons" WHERE "lessons"."unit_id" = 3 AND  "lessons"."id" = ? LIMIT 1 [["id", "12"]] 
Rendered lessons/_form.html.erb (3.4ms) 
Rendered lessons/edit.html.erb within layouts/application (4.4ms) 
Completed 500 Internal Server Error in 7ms 

ActionController::RoutingError (No route matches {:action=>"show", :controller=>"courses", :id=>nil}): 
app/views/lessons/edit.html.erb:8:in `_app_views_lessons_edit_html_erb___3830769233446763788_70188197130200' 

回答

0

發現了它。該鏈接是一個表體的一部分,用一個.each迭代器構建。我需要將名稱從「@lesson」更改爲「lesson」,因爲這是構建表時引用的名稱。

對於其他人遇到類似問題,我已經將功能代碼放在下面。

<tbody> 
    <% @unit.lessons.each do |lesson| %> 
    <tr> 
     <td><%= link_to lesson.lesson_name, [@unit, lesson] %></td> 
     . 
     . 
     . 
     <td><%= link_to 'Edit', edit_unit_lesson_path(@unit, lesson) %></td> 
    </tr> 
<% end %> 
</tbody>