I have a very simple case of nested resources but I am having trouble to get them to work.
My models:
class TodoList < ActiveRecord::Base
has_many :todo_items, dependent: :destroy
class TodoItem < ActiveRecord::Base
belongs_to :todo_list
My controller:
class TodoItemsController < ApplicationController
before_action :set_todo_list
before_action :set_todo_item, only: [:show, :edit, :update, :destroy]
def show
end
private
def set_todo_item
@todo_item = @todo_list.todo_items.find(params[:id])
end
def set_todo_list
@todo_list = TodoList.find(params[:todo_list_id])
end
My show.html.erb:
<%= link_to 'Edit', edit_todo_list_todo_item_path([@todo_item.todo_list, @todo_item]) %>
I got the error
"No route matches {:action=>"edit", :controller=>"todo_items", :id=>nil, ..., missing required keys: [:id].
I know the todo_item_id is missing, but I couldn't figure out why. When I debug, I saw that both @todo_list and @todo_item were getting values. But as soon as the @todo_item was assigned, this error would rise. What did I do wrong? How can I correct this? Any insights will be appreciated.
Try this:
<%= link_to 'Edit', edit_todo_list_todo_item_path(@todo_item.todo_list, @todo_item) %>
or:
<%= link_to 'Edit', edit_todo_list_todo_item_path(todo_list_id: @todo_item.todo_list.id, id: @todo_item.id) %>