如何解决以下代码的路由错误?

时间:2017-09-18 08:36:41

标签: ruby-on-rails ruby

我很难修复路由路径。例如,我正在尝试创建一个创建新课程页面的路径。

<li><%= link_to "new lesson", course_lesson_path %></li>

我收到错误说

No route matches {:action=>"show", :controller=>"lessons"}, missing required keys: [:course_id, :id]
        new_user_session GET    /users/sign_in(.:format)                       devise/sessions#new
            user_session POST   /users/sign_in(.:format)                       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)                      devise/sessions#destroy
       new_user_password GET    /users/password/new(.:format)                  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)                 devise/passwords#edit
           user_password PATCH  /users/password(.:format)                      devise/passwords#update
                         PUT    /users/password(.:format)                      devise/passwords#update
                         POST   /users/password(.:format)                      devise/passwords#create
cancel_user_registration GET    /users/cancel(.:format)                        devise/registrations#cancel
   new_user_registration GET    /users/sign_up(.:format)                       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)                          devise/registrations#edit
       user_registration PATCH  /users(.:format)                               devise/registrations#update
                         PUT    /users(.:format)                               devise/registrations#update
                         DELETE /users(.:format)                               devise/registrations#destroy
                         POST   /users(.:format)                               devise/registrations#create
                    root GET    /                                              devise/registrations#new
          course_lessons GET    /courses/:course_id/lessons(.:format)          lessons#index
                         POST   /courses/:course_id/lessons(.:format)          lessons#create
       new_course_lesson GET    /courses/:course_id/lessons/new(.:format)      lessons#new
      edit_course_lesson GET    /courses/:course_id/lessons/:id/edit(.:format) lessons#edit
           course_lesson GET    /courses/:course_id/lessons/:id(.:format)      lessons#show
                         PATCH  /courses/:course_id/lessons/:id(.:format)      lessons#update
                         PUT    /courses/:course_id/lessons/:id(.:format)      lessons#update
                         DELETE /courses/:course_id/lessons/:id(.:format)      lessons#destroy
                 courses GET    /courses(.:format)                             courses#index
                         POST   /courses(.:format)                             courses#create
              new_course GET    /courses/new(.:format)                         courses#new
             edit_course GET    /courses/:id/edit(.:format)                    courses#edit
                  course GET    /courses/:id(.:format)                         courses#show
                         PATCH  /courses/:id(.:format)                         courses#update
                         PUT    /courses/:id(.:format)                         courses#update
                         DELETE /courses/:id(.:format)                         courses#destroy

我在思考我创建模型的方式并不好,因为我有一个课程模型和一个课程模型,我使用了课程:我的课程模型属性中的参考和那种混乱的事情。

3 个答案:

答案 0 :(得分:1)

在您的情况下,如果您要创建重定向到新/courses/:course_id/lessons/new路径的链接,则需要使用new_course_lesson_path传递所需的course_id,例如:

<li><%= link_to "new lesson", new_course_lesson_path(id: 1) %></li> <!-- 1 as example -->

您看到missing required keys: [:course_id, :id]错误的原因是因为course_lesson_path已定义为在参数内接收两个值:

/courses/:course_id/lessons/:id # :course_id and :id

在这种情况下,它会像:

<li><%= link_to "new lesson", course_lesson_path(course_id: 1, id: 1) %></li> <!-- 1 and 1 as example -->

答案 1 :(得分:1)

course_lesson_path因为您使用它指向显示课程的操作(当然),即它需要course_id(父级的ID)和课程id

并且您想要指向新的课程路径,因此您需要使用new_course_lesson_path指定课程的ID,例如:
<li><%= link_to "new lesson", new_course_lesson_path(@course_id) %></li>
@course_id当然是你想要创造这一课的内容。

答案 2 :(得分:1)

lessons_controller.rb
# need to load course from controller
def new 
  @course = Course.find(:course_id)
end

In your new.erb.html

<li><%= link_to "new lesson", course_lesson_path(@course) %></li>