Used Turbo Frames/Streams for adding/deleting tasks without refresh entire browser
This commit is contained in:
@@ -1,53 +1,34 @@
|
||||
class TasksController < ApplicationController
|
||||
def index
|
||||
@project = Project.find(params[:project_id])
|
||||
@tasks = @project.tasks
|
||||
end
|
||||
before_action :set_project
|
||||
|
||||
def show
|
||||
@project = Project.find(params[:project_id])
|
||||
@task = @project.tasks.find(params[:id])
|
||||
end
|
||||
def create
|
||||
@task = @project.tasks.build(task_params)
|
||||
|
||||
def new
|
||||
@project = Project.find(params[:project_id])
|
||||
@task = @project.tasks.new
|
||||
end
|
||||
|
||||
def create
|
||||
@project = Project.find(params[:project_id])
|
||||
@task = @project.tasks.new(task_params)
|
||||
if @task.save
|
||||
redirect_to project_task_path(@project, @task)
|
||||
if @task.save
|
||||
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
|
||||
end
|
||||
|
||||
def edit
|
||||
@project = Project.find(params[:project_id])
|
||||
@task = @project.tasks.find(params[:id])
|
||||
end
|
||||
def destroy
|
||||
@task = @project.tasks.find(params[:id])
|
||||
@task.destroy
|
||||
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
|
||||
private
|
||||
|
||||
def destroy
|
||||
@project = Project.find(params[:project_id])
|
||||
@task = @project.tasks.find(params[:id])
|
||||
@task.destroy
|
||||
redirect_to project_tasks_path(@project)
|
||||
end
|
||||
def set_project
|
||||
@project = Project.find(params[:project_id])
|
||||
end
|
||||
|
||||
private
|
||||
def task_params
|
||||
params.require(:task).permit(:title, :description, :status)
|
||||
end
|
||||
def task_params
|
||||
params.require(:task).permit(:title, :status)
|
||||
end
|
||||
end
|
||||
|
||||
2
app/helpers/tasks_helper.rb
Normal file
2
app/helpers/tasks_helper.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
module TasksHelper
|
||||
end
|
||||
@@ -10,3 +10,6 @@
|
||||
} %>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
||||
<br>
|
||||
<h2><%= link_to "Add Project", new_project_path %></h2>
|
||||
@@ -1,16 +1,21 @@
|
||||
<h1><%= @project.project_name %></h1>
|
||||
<p><%= @project.description %></p>
|
||||
|
||||
<ul>
|
||||
<% @project.tasks.each do |task| %>
|
||||
<li>
|
||||
<%= task.title %> (<%= task.status %>)
|
||||
</li>
|
||||
<%= link_to "Destroy", project_task_path(@project, task), data: {
|
||||
turbo_method: :delete,
|
||||
turbo_confirm: "Are you sure?"
|
||||
} %>
|
||||
<% end %>
|
||||
</ul>
|
||||
<turbo-frame id="tasks">
|
||||
<ul id="tasks_list">
|
||||
<% @project.tasks.each do |task| %>
|
||||
<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 %>
|
||||
</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>
|
||||
13
app/views/tasks/_form.html.erb
Normal file
13
app/views/tasks/_form.html.erb
Normal 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 %>
|
||||
13
app/views/tasks/create.turbo_stream.erb
Normal file
13
app/views/tasks/create.turbo_stream.erb
Normal 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 %>
|
||||
1
app/views/tasks/destroy.turbo_stream.erb
Normal file
1
app/views/tasks/destroy.turbo_stream.erb
Normal file
@@ -0,0 +1 @@
|
||||
<%= turbo_stream.remove dom_id(@task) %>
|
||||
@@ -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 %>
|
||||
8
test/controllers/projects_controller_test.rb
Normal file
8
test/controllers/projects_controller_test.rb
Normal 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
|
||||
7
test/controllers/tasks_controller_test.rb
Normal file
7
test/controllers/tasks_controller_test.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
require "test_helper"
|
||||
|
||||
class TasksControllerTest < ActionDispatch::IntegrationTest
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
||||
Reference in New Issue
Block a user