This page is part of a series that describes building a complete todo list application with LiveComponent. Please consider reading it from the beginning.
<div class="TodoItem"> <% if @editing %> <%= form_with(model: @todo_item) do |f| %> <%= f.text_field :text %> <%= f.submit %> <% end %> <% else %> <%= @todo_item.text %> <%= button_tag( "Edit", data: { action: "click->todoitemcomponent#edit" } ) %> <%= button_to( "Delete", todo_list_todo_item_path(@todo_item.todo_list_id, @todo_item), rerender: :parent, method: :delete, form: { data: { turbo: true, turbo_stream: true } } ) %> <% end %> </div>
<div class="TodoItem"> <% if @editing %> <%= form_with(model: @todo_item) do |f| %> <%= f.text_field :text %> <%= f.submit %> <% end %> <% else %> <%= @todo_item.text %> <%= button_tag( "Edit", data: { action: "click->todoitemcomponent#edit" } ) %> <%= button_to( "Delete", todo_list_todo_item_path(@todo_item.todo_list_id, @todo_item), rerender: :parent, method: :delete, form: { data: { turbo: true, turbo_stream: true } } ) %> <% end %> </div>
class TodoItemsController < ApplicationController def destroy TodoItem.delete(params[:id]) end end
class TodoItemsController < ApplicationController def destroy TodoItem.delete(params[:id]) end end
<%= live.rerender do |todo_list_component| %> <% todo_list_component.todo_items.reject! do |todo_item_component| %> <% todo_item_component.todo_item.id == params[:id].to_i %> <% end %> <% end %>
<%= live.rerender do |todo_list_component| %> <% todo_list_component.todo_items.reject! do |todo_item_component| %> <% todo_item_component.todo_item.id == params[:id].to_i %> <% end %> <% end %>
class TodoItemComponent < ApplicationComponent include LiveComponent::Base serializes :todo_item, with: :model_serializer, attributes: [:text, :todo_list_id] attr_reader :todo_item, :editing def initialize(todo_item:, editing: false) @todo_item = todo_item @editing = editing end end
class TodoItemComponent < ApplicationComponent include LiveComponent::Base serializes :todo_item, with: :model_serializer, attributes: [:text, :todo_list_id] attr_reader :todo_item, :editing def initialize(todo_item:, editing: false) @todo_item = todo_item @editing = editing end end
<%= render(TodoListComponent.new(todo_list: @todo_list.attributes)) do |todo_list_component| %> <% @todo_list.todo_items.each do |todo_item| %> <% todo_list_component.with_todo_item(todo_item: todo_item.attributes) %> <% end %> <% end %>
<%= render(TodoListComponent.new(todo_list: @todo_list.attributes)) do |todo_list_component| %> <% @todo_list.todo_items.each do |todo_item| %> <% todo_list_component.with_todo_item(todo_item: todo_item.attributes) %> <% end %> <% end %>