Used Turbo Frames/Streams for adding/deleting tasks without refresh entire browser

This commit is contained in:
KaungKaung
2026-01-28 14:12:24 +06:30
parent 910c1912e0
commit 0f5d105be3
10 changed files with 87 additions and 76 deletions

View File

@@ -1,53 +1,34 @@
class TasksController < ApplicationController
def index
@project = Project.find(params[:project_id])
@tasks = @project.tasks
end
def show
@project = Project.find(params[:project_id])
@task = @project.tasks.find(params[:id])
end
def new
@project = Project.find(params[:project_id])
@task = @project.tasks.new
end
before_action :set_project
def create
@project = Project.find(params[:project_id])
@task = @project.tasks.new(task_params)
@task = @project.tasks.build(task_params)
if @task.save
redirect_to project_task_path(@project, @task)
respond_to do |format|
format.turbo_stream
format.html { redirect_to @project, notice: "Task created!" }
end
else
render :new
respond_to do |format|
format.turbo_stream { render turbo_stream: turbo_stream.replace("new_task", partial: "tasks/form", locals: { project: @project, task: @task }) }
format.html { render "projects/show", status: :unprocessable_entity }
end
end
def edit
@project = Project.find(params[:project_id])
@task = @project.tasks.find(params[:id])
end
def update
@project = Project.find(params[:project_id])
@task = @project.tasks.find(params[:id])
if @task.update(task_params)
redirect_to project_task_path(@project, @task)
else
render :edit
end
end
def destroy
@project = Project.find(params[:project_id])
@task = @project.tasks.find(params[:id])
@task.destroy
redirect_to project_tasks_path(@project)
end
private
def set_project
@project = Project.find(params[:project_id])
end
def task_params
params.require(:task).permit(:title, :description, :status)
params.require(:task).permit(:title, :status)
end
end

View File

@@ -0,0 +1,2 @@
module TasksHelper
end

View File

@@ -10,3 +10,6 @@
} %>
<% end %>
</ul>
<br>
<h2><%= link_to "Add Project", new_project_path %></h2>

View File

@@ -1,16 +1,21 @@
<h1><%= @project.project_name %></h1>
<p><%= @project.description %></p>
<ul>
<turbo-frame id="tasks">
<ul id="tasks_list">
<% @project.tasks.each do |task| %>
<li>
<li id="<%= dom_id(task) %>">
<%= task.title %> (<%= task.status %>)
</li>
<%= link_to "Destroy", project_task_path(@project, task), data: {
turbo_method: :delete,
turbo_confirm: "Are you sure?"
} %>
</li>
<% end %>
</ul>
</turbo-frame>
<%= link_to "Add Task", new_project_task_path(@project) %>
<turbo-frame id="new_task">
<%= render partial: "tasks/form", locals: { project: @project, task: Task.new } %>
</turbo-frame>

View File

@@ -0,0 +1,13 @@
<%= form_with model: [project, task], local: false do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :status %><br>
<%= f.select :status, Task.statuses.keys %>
</p>
<%= f.submit "Create Task" %>
<% end %>

View File

@@ -0,0 +1,13 @@
<%= turbo_stream.append "tasks_list" do %>
<li id="<%= dom_id(@task) %>">
<%= @task.title %> (<%= @task.status %>)
<%= link_to "Destroy", project_task_path(@project, @task), data: {
turbo_method: :delete,
turbo_confirm: "Are you sure?"
} %>
</li>
<% end %>
<%= turbo_stream.replace "new_task" do %>
<%= render partial: "tasks/form", locals: { project: @project, task: Task.new } %>
<% end %>

View File

@@ -0,0 +1 @@
<%= turbo_stream.remove dom_id(@task) %>

View File

@@ -1,22 +0,0 @@
<h3>Add new task for "<%= @project.project_name %>"</h3>
<%= form_with model: [@project, Task.new], local: true do |task| %>
<p>
<%= task.label :title %><br>
<%= task.text_field :title %>
<% @project.tasks.each do |t| %>
<% t.errors.full_messages_for(:title).each do |message| %>
<p><%= message %></p>
<% end %>
<% end %>
</p>
<p>
<%= task.label :status %><br>
<%= task.select :status, Task.statuses.keys %>
</p>
<p>
<%= task.submit "Create Task" %>
</p>
<% end %>

View File

@@ -0,0 +1,8 @@
require "test_helper"
class ProjectsControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
get projects_index_url
assert_response :success
end
end

View File

@@ -0,0 +1,7 @@
require "test_helper"
class TasksControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end