Trying to call an edit (via a button in the todo_list show view) of the todo_item. Currently getting the error below.
I’ve been cribbing from the getting started rails guide, but creating more tailored views that show here is the todo_list and then here are all the associated todo_items on that list in a single view.
class TodoItemsController < ApplicationController
before_action :set_todo_list
def create
@todo_item = @todo_list.todo_items.create(todo_item_params)
redirect_to todo_list_path(@todo_list)
end
def show
@todo_item = @todo_list.todo_items.find(params[:id])
end
def edit
@todo_item = @todo_list.todo_items.find(params[:id])
end
def update
@todo_item = @todo_list.todo_items.find(params[:id])
if @todo_item.update(todo_item_params)
redirect_to todo_list_path(@todo_list)
else
render :edit, status: :unprocessable_entity
end
end
def destroy
@todo_item = @todo_list.todo_items.find(params[:id]).destroy
redirect_to todo_list_path, status: :see_other
end
private
def todo_item_params
params.require(:todo_item).permit(:description)
end
def set_todo_list
@todo_list = TodoList.find(params[:todo_list_id])
end
end
<h1><%= @todo_list.title %></h1>
<p><%= @todo_list.description %></p>
<div>
<table class = "table">
<thead class="thead-dark">
<th>
Description
<tbody>
<% @todo_list.todo_items.each do |todo_item| %>
<tr>
<td><%= link_to todo_item.description, [todo_item.todo_list, todo_item] %></td>
<td><%= link_to "Edit", edit_todo_list_todo_item_path([todo_item.todo_list, todo_item]), class: "btn btn-primary" %></td>
<td><%= link_to "Delete", [todo_item.todo_list, todo_item], data: { turbo_method: :delete, turbo_confirm: "Are you sure?"}, class: "btn btn-danger" %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<h2>Add a todo item:</h2>
<%= form_with model: [ @todo_list, @todo_list.todo_items.build ] do |form| %>
<p>
<%= form.label :description %><br>
<%= form.text_field :description %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<%= link_to "Back to todo lists", root_path %>
class TodoListsController < ApplicationController
def index
@todo_lists = TodoList.all
end
def show
@todo_list = TodoList.find(params[:id])
end
def new
@todo_list = TodoList.new
end
def create
@todo_list = TodoList.new(todo_list_params)
if @todo_list.save
redirect_to todo_lists_path
else
render :new, status: :unprocessable_entity
end
end
def edit
@todo_list = TodoList.find(params[:id])
end
def update
@todo_list = TodoList.find(params[:id])
if @todo_list.update(todo_list_params)
redirect_to todo_lists_path
else
render :edit, status: :unprocessable_entity
end
end
def destroy
@todo_list = TodoList.find(params[:id]).destroy
redirect_to root_path, status: :see_other
end
private
def todo_list_params
params.require(:todo_list).permit(:title, :description)
end
end