Timothé Gauguet
Timothé Gauguet

Reputation: 183

missing required keys: [:id] rails

when I try to run my code, I got the problem above. Here's my 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 %>

my tasks controller :

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

and my routes :

resources :to_do_lists do
    resources :tasks
end

full error :

No route matches {:action=>"show", :controller=>"tasks", :id=>nil, :to_do_list_id=>"157"} missing required keys: [:id]

Any idea ?

EDIT to_do_list's controller :

def show
    @list = @user.to_do_lists.find(params[:id])
    @task = @list.tasks.new
    @build = @list.tasks.build
end

EDIT 2 full error :

[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'

Upvotes: 1

Views: 959

Answers (1)

Frederick Cheung
Frederick Cheung

Reputation: 84114

It's because you are doing this:

@build = @list.tasks.build

This adds an unsaved task object to the list.tasks collection. Since it is unsaved, it's id is nil and can't be linked to.

Alternatively, skip the "delete" link if the task is not saved (persisted? is false).

Upvotes: 4

Related Questions