I am making an events app where each event can have multiple to do lists each with their own tasks.
I am struggling with the third level nesting of the tasks (todo_items)
Here are my models:
Event.rb
has_many :todo_lists, :dependent => :destroy
has_many :todo_items, :through => :todo_lists
accepts_nested_attributes_for :todo_lists
accepts_nested_attributes_for :todo_items
Todo_list.rb
has_many :todo_items, :dependent => :destroy
accepts_nested_attributes_for :todo_items
belongs_to :event
Todo_item.rb
belongs_to :todo_list
Todo_list_Controller.rb
def set_todo_list
@todo_list = @event.todo_lists.find(params[:id])
end
def set_event
@event = Event.find(params[:event_id])
end
def todo_list_params
params.require(:todo_list).permit(:title, :description, :event_id)
end
Todo_items_Controller.rb
def set_todo_list
@todo_list = TodoList.find(params[:todo_list_id])
end
def set_todo_item
@todo_item = @todo_list.todo_items.find(params[:id])
end
def todo_item_params
params[:todo_item].permit(:content, :todo_list_id)
end
routes.rb
resources :events do
resources :todo_lists do
resources :todo_items do
member do
patch :complete
end
end
end
end
If any other code is needed I Will update! Thanks in advance!
I have done my best to nest the objects. I can add tasks (todo_items) to a todo_list but I cannot delete or complete them.
The error comes from the todo_LIST_controller not the todo_ITEMS_controller
Error Log:
ActiveRecord::RecordNotFound (Couldn't find TodoList with 'id'=5 [WHERE "todo_lists"."event_id" = ?]):