Basic turbo todo list implementation

Based on:

https://webcrunch.com/posts/digging-into-turbo-with-ruby-on-rails-7
This commit is contained in:
2024-07-28 23:53:35 +02:00
parent 84e6e43812
commit ea5d597fc7
8 changed files with 72 additions and 66 deletions

View File

@@ -1,9 +1,9 @@
class TodosController < ApplicationController
before_action :set_todo, only: %i[ show edit update destroy ]
before_action :set_todo, only: %i[show edit update destroy]
# GET /todos or /todos.json
def index
@todos = Todo.all
@todos = Todo.in_order_of(:status, %w[incomplete complete])
end
# GET /todos/1 or /todos/1.json
@@ -25,11 +25,14 @@ class TodosController < ApplicationController
respond_to do |format|
if @todo.save
format.html { redirect_to todo_url(@todo), notice: "Todo was successfully created." }
format.json { render :show, status: :created, location: @todo }
format.turbo_stream
format.html { redirect_to todo_url(@todo), notice: 'Todo was successfully created.' }
else
format.turbo_stream do
render turbo_stream: turbo_stream.replace("#{helpers.dom_id(@todo)}_form", partial: 'form',
locals: { todo: @todo })
end
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @todo.errors, status: :unprocessable_entity }
end
end
end
@@ -38,11 +41,13 @@ class TodosController < ApplicationController
def update
respond_to do |format|
if @todo.update(todo_params)
format.html { redirect_to todo_url(@todo), notice: "Todo was successfully updated." }
format.json { render :show, status: :ok, location: @todo }
format.html { redirect_to todo_url(@todo), notice: 'Todo was successfully updated.' }
else
format.turbo_stream do
render turbo_stream: turbo_stream.replace("#{helpers.dom_id(@todo)}_form", partial: 'form',
locals: { todo: @todo })
end
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @todo.errors, status: :unprocessable_entity }
end
end
end
@@ -52,19 +57,20 @@ class TodosController < ApplicationController
@todo.destroy!
respond_to do |format|
format.html { redirect_to todos_url, notice: "Todo was successfully destroyed." }
format.json { head :no_content }
format.turbo_stream { render turbo_stream: turbo_stream.remove("#{helpers.dom_id(@todo)}") }
format.html { redirect_to todos_url, notice: 'Todo was successfully destroyed.' }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_todo
@todo = Todo.find(params[:id])
end
# Only allow a list of trusted parameters through.
def todo_params
params.require(:todo).permit(:title, :status)
end
# Use callbacks to share common setup or constraints between actions.
def set_todo
@todo = Todo.find(params[:id])
end
# Only allow a list of trusted parameters through.
def todo_params
params.require(:todo).permit(:title, :status)
end
end