2012-08-02 101 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>