Ruby on rails 缺少必需的键:[:id]rails

Ruby on rails 缺少必需的键:[:id]rails,ruby-on-rails,Ruby On Rails,当我试图运行我的代码时,我遇到了上面的问题。 这是我的任务/_task.html.erb 我的路线是: resources :to_do_lists do resources :tasks end 完全错误: No route matches {:action=>"show", :controller=>"tasks", :id=>nil, :to_do_list_id=>"157"} missing required keys: [:id] [localho

当我试图运行我的代码时,我遇到了上面的问题。 这是我的任务/_task.html.erb

我的路线是:

resources :to_do_lists do
    resources :tasks
end
完全错误:

No route matches {:action=>"show", :controller=>"tasks", :id=>nil, :to_do_list_id=>"157"} missing required keys: [:id]
[localhost] [::1] [6d1ad8d6-bfdf-4c] Started GET "/to_do_lists/158" for ::1 at 2016-05-07 09:14:37 +0200
[localhost] [::1] [6d1ad8d6-bfdf-4c] Started GET "/to_do_lists/158" for ::1 at 2016-05-07 09:14:37 +0200
[localhost] [::1] [6d1ad8d6-bfdf-4c] Processing by     ToDoListsController#show as HTML
[localhost] [::1] [6d1ad8d6-bfdf-4c] Processing by ToDoListsController#show as HTML
[localhost] [::1] [6d1ad8d6-bfdf-4c]   Parameters: {"id"=>"158"}
[localhost] [::1] [6d1ad8d6-bfdf-4c]   Parameters: {"id"=>"158"}
[localhost] [::1] [6d1ad8d6-bfdf-4c]   User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 24]]
[localhost] [::1] [6d1ad8d6-bfdf-4c]   User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 24]]
[localhost] [::1] [6d1ad8d6-bfdf-4c]   ToDoList Load (0.4ms)  SELECT  "to_do_lists".* FROM "to_do_lists" WHERE "to_do_lists"."user_id" = $1 AND "to_do_lists"."id" = $2 LIMIT 1  [["user_id", 24], ["id", 158]]
[localhost] [::1] [6d1ad8d6-bfdf-4c]   ToDoList Load (0.4ms)  SELECT  "to_do_lists".* FROM "to_do_lists" WHERE "to_do_lists"."user_id" = $1 AND "to_do_lists"."id" = $2 LIMIT 1  [["user_id", 24], ["id", 158]]
[localhost] [::1] [6d1ad8d6-bfdf-4c]    (0.3ms)  SELECT COUNT(*) FROM "tasks" WHERE "tasks"."to_do_list_id" = $1  [["to_do_list_id", 158]]
[localhost] [::1] [6d1ad8d6-bfdf-4c]    (0.3ms)  SELECT COUNT(*) FROM "tasks" WHERE "tasks"."to_do_list_id" = $1  [["to_do_list_id", 158]]
[localhost] [::1] [6d1ad8d6-bfdf-4c]   Task Load (0.4ms)  SELECT "tasks".* FROM "tasks" WHERE "tasks"."to_do_list_id" = $1   [["to_do_list_id", 158]]
[localhost] [::1] [6d1ad8d6-bfdf-4c]   Task Load (0.4ms)  SELECT "tasks".* FROM "tasks" WHERE "tasks"."to_do_list_id" = $1  [["to_do_list_id", 158]]
[localhost] [::1] [6d1ad8d6-bfdf-4c]   Rendered tasks/_task.html.erb (5.3ms)
[localhost] [::1] [6d1ad8d6-bfdf-4c]   Rendered tasks/_task.html.erb (5.3ms)
[localhost] [::1] [6d1ad8d6-bfdf-4c]   Rendered to_do_lists/show.html.erb within layouts/application (7.6ms)
[localhost] [::1] [6d1ad8d6-bfdf-4c]   Rendered to_do_lists/show.html.erb within layouts/application (7.6ms)
[localhost] [::1] [6d1ad8d6-bfdf-4c] Completed 500 Internal Server Error in 17ms (ActiveRecord: 1.4ms)
[localhost] [::1] [6d1ad8d6-bfdf-4c] Completed 500 Internal Server Error in 17ms (ActiveRecord: 1.4ms)
[localhost] [::1] [6d1ad8d6-bfdf-4c]
ActionView::Template::Error (No route matches {:action=>"show", :controller=>"tasks", :id=>nil, :to_do_list_id=>"158"} missing required keys: [:id]):
2:  <% @list.tasks.each do |task| %>
3:      <p>
4:          <%= task.name %><%= task.id %>
5:          <%= link_to "Supprimer", to_do_list_task_path(@list, task), method: :delete %>
6:      </p>
7:  <% end %>
8: </div>
  app/views/tasks/_task.html.erb:5:in `block in    _app_views_tasks__task_html_erb___979180469130099435_70290175534220'
  app/views/tasks/_task.html.erb:2:in `_app_views_tasks__task_html_erb___979180469130099435_70290175534220'
  app/views/to_do_lists/show.html.erb:7:in `_app_views_to_do_lists_show_html_erb__2820495566946140584_70290199271140'
有什么想法吗

编辑到列表的控制器:

def destroy
    @list = ToDoList.find(params[:to_do_list_id])
    @task = @list.tasks.find(params[:id])
    if @task.destroy
        redirect_to(:back)
    else
        redirect_to(:back)
    end
end
def show
    @list = @user.to_do_lists.find(params[:id])
    @task = @list.tasks.new
    @build = @list.tasks.build
end
编辑2完整错误:

No route matches {:action=>"show", :controller=>"tasks", :id=>nil, :to_do_list_id=>"157"} missing required keys: [:id]
[localhost] [::1] [6d1ad8d6-bfdf-4c] Started GET "/to_do_lists/158" for ::1 at 2016-05-07 09:14:37 +0200
[localhost] [::1] [6d1ad8d6-bfdf-4c] Started GET "/to_do_lists/158" for ::1 at 2016-05-07 09:14:37 +0200
[localhost] [::1] [6d1ad8d6-bfdf-4c] Processing by     ToDoListsController#show as HTML
[localhost] [::1] [6d1ad8d6-bfdf-4c] Processing by ToDoListsController#show as HTML
[localhost] [::1] [6d1ad8d6-bfdf-4c]   Parameters: {"id"=>"158"}
[localhost] [::1] [6d1ad8d6-bfdf-4c]   Parameters: {"id"=>"158"}
[localhost] [::1] [6d1ad8d6-bfdf-4c]   User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 24]]
[localhost] [::1] [6d1ad8d6-bfdf-4c]   User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 24]]
[localhost] [::1] [6d1ad8d6-bfdf-4c]   ToDoList Load (0.4ms)  SELECT  "to_do_lists".* FROM "to_do_lists" WHERE "to_do_lists"."user_id" = $1 AND "to_do_lists"."id" = $2 LIMIT 1  [["user_id", 24], ["id", 158]]
[localhost] [::1] [6d1ad8d6-bfdf-4c]   ToDoList Load (0.4ms)  SELECT  "to_do_lists".* FROM "to_do_lists" WHERE "to_do_lists"."user_id" = $1 AND "to_do_lists"."id" = $2 LIMIT 1  [["user_id", 24], ["id", 158]]
[localhost] [::1] [6d1ad8d6-bfdf-4c]    (0.3ms)  SELECT COUNT(*) FROM "tasks" WHERE "tasks"."to_do_list_id" = $1  [["to_do_list_id", 158]]
[localhost] [::1] [6d1ad8d6-bfdf-4c]    (0.3ms)  SELECT COUNT(*) FROM "tasks" WHERE "tasks"."to_do_list_id" = $1  [["to_do_list_id", 158]]
[localhost] [::1] [6d1ad8d6-bfdf-4c]   Task Load (0.4ms)  SELECT "tasks".* FROM "tasks" WHERE "tasks"."to_do_list_id" = $1   [["to_do_list_id", 158]]
[localhost] [::1] [6d1ad8d6-bfdf-4c]   Task Load (0.4ms)  SELECT "tasks".* FROM "tasks" WHERE "tasks"."to_do_list_id" = $1  [["to_do_list_id", 158]]
[localhost] [::1] [6d1ad8d6-bfdf-4c]   Rendered tasks/_task.html.erb (5.3ms)
[localhost] [::1] [6d1ad8d6-bfdf-4c]   Rendered tasks/_task.html.erb (5.3ms)
[localhost] [::1] [6d1ad8d6-bfdf-4c]   Rendered to_do_lists/show.html.erb within layouts/application (7.6ms)
[localhost] [::1] [6d1ad8d6-bfdf-4c]   Rendered to_do_lists/show.html.erb within layouts/application (7.6ms)
[localhost] [::1] [6d1ad8d6-bfdf-4c] Completed 500 Internal Server Error in 17ms (ActiveRecord: 1.4ms)
[localhost] [::1] [6d1ad8d6-bfdf-4c] Completed 500 Internal Server Error in 17ms (ActiveRecord: 1.4ms)
[localhost] [::1] [6d1ad8d6-bfdf-4c]
ActionView::Template::Error (No route matches {:action=>"show", :controller=>"tasks", :id=>nil, :to_do_list_id=>"158"} missing required keys: [:id]):
2:  <% @list.tasks.each do |task| %>
3:      <p>
4:          <%= task.name %><%= task.id %>
5:          <%= link_to "Supprimer", to_do_list_task_path(@list, task), method: :delete %>
6:      </p>
7:  <% end %>
8: </div>
  app/views/tasks/_task.html.erb:5:in `block in    _app_views_tasks__task_html_erb___979180469130099435_70290175534220'
  app/views/tasks/_task.html.erb:2:in `_app_views_tasks__task_html_erb___979180469130099435_70290175534220'
  app/views/to_do_lists/show.html.erb:7:in `_app_views_to_do_lists_show_html_erb__2820495566946140584_70290199271140'

这是因为你正在这样做:

@build = @list.tasks.build
这会将未保存的任务对象添加到list.tasks集合中。由于未保存,其id为零,无法链接到


或者,如果任务未保存,是否跳过删除链接?是错误的。

这是因为您正在这样做:

@build = @list.tasks.build
这会将未保存的任务对象添加到list.tasks集合中。由于未保存,其id为零,无法链接到


或者,如果任务未保存,是否跳过删除链接?为false。

呈现此视图的控制器操作是什么样子的?我刚刚添加了控制器action@TimothéGauguet,请从服务器控制台提供完整错误。呈现此视图的控制器操作是什么样子的?我刚刚添加了控制器action@Timoth高盖特,请从服务器控制台提供完整错误。我认为任务对象已保存,因为viewtask.id工作正常-对于未保存的任务,它只返回零确定,谢谢!我怎样才能解决这个问题?我试图删除@build并将其逻辑放在视图中,但问题仍然存在。您需要停止调用list.tasks.build。如果您需要某项未保存的任务,请使用Task.new,或者不呈现未保存任务的删除链接。我认为任务对象已保存,因为viewtask.id工作正常-对于未保存的任务,它只返回nil确定,谢谢!我怎样才能解决这个问题?我试图删除@build并将其逻辑放在视图中,但问题仍然存在。您需要停止调用list.tasks.build。如果您需要某项任务的未保存任务,或者不呈现未保存任务的删除链接,请使用Task.new。