当我尝试运行我的代码时,我遇到了上面的问题。
这是我的tasks/_task.html.erb
<% @list.tasks.each do |task| %>
<p>
<%= task.name %><%= task.id %>
<%= link_to "Destroy", to_do_list_task_path(@list, task.id), method: :delete %>
</p>
<% end %>
我的任务控制器:
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
和我的路线:
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]
有什么想法吗?
编辑to_do_list的控制器:
def show
@list = @user.to_do_lists.find(params[:id])
@task = @list.tasks.new
@build = @list.tasks.build
end
编辑2完整错误:
[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'
答案 0 :(得分:4)
这是因为你这样做:
@build = @list.tasks.build
这会将未保存的任务对象添加到list.tasks
集合中。由于它是未保存的,因此它的id为nil且无法链接。
或者,如果未保存任务(persisted?
为false),请跳过“删除”链接。