I have a tasks list under a project model. I am trying to have a checkbox and when clicked to complete the task, call a method called toggle, update the task then replace the record via hotwire and display additional information about who completed it and at what time.
When I click the checkbox it does complete the task. That part of it is working. I am just having a hard time replacing the record via turbo stream broadcast. I am getting a Nil location provided that errors out on the checkbox tag.
As far as turbo_stream format goes, should I be triggering the replace from the update method of the task, or from the toggle method of the task?
Task.rb
<div class="table-row" id="<%= dom_id task %>" data-id="<%= task.id %>">
<div>
<%= form_with(model: [@project, @task]) do |form| %><%= form.check_box :completed,
data: {
id: task.id,
action: "tasks#toggle"
},checked: task.completed %>
<% end %>
</div>
...
Projects show:
...
<%= turbo_stream_from "tasks" %>
<div class="overflow-hidden">
<div class="table border-collapse table-auto w-full divide-y divide-gray-200 dark:divide-gray-700">
<div class="table-row-group divide-y divide-gray-200 bg-white dark:divide-gray-700 dark:bg-gray-800"
id="tasks" data-controller="drag" data-drag-url="/projects/<%= @project.id %>/tasks/:id/move">
<%= render @tasks.order('position asc') %>
</div>
</div>
</div>
...
tasks controller
class TasksController < ApplicationController
before_action :set_task, except: [:create]
before_action :set_project, except: [:toggle]
# GET /tasks or /tasks.json
def index
@tasks = Task.all
@task = Task.new
end
# GET /tasks/1 or /tasks/1.json
def show
end
# GET /tasks/new
def new
@task = Task.new
end
# GET /tasks/1/edit
def edit
end
# POST /tasks or /tasks.json
def create
@task = @project.tasks.build(task_params)
@task.user_id = current_user.id
respond_to do |format|
if @task.save
format.turbo_stream
format.html { redirect_to project_task_path(@project, @task), notice: "Task was successfully created." }
format.json { render :show, status: :created, location: @task }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tasks/1 or /tasks/1.json
def update
respond_to do |format|
if @task.update(task_params)
format.html { redirect_to project_task_path(@project, @task), notice: "Task was successfully updated." }
format.json { render :show, status: :ok, location: @task }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tasks/1 or /tasks/1.json
def destroy
@task.destroy
respond_to do |format|
format.html { redirect_to tasks_url, notice: "Task was successfully destroyed." }
format.json { head :no_content }
end
end
def toggle
@task = Task.find(params[:id])
@task.update(completed: params[:completed])
end
def move
@task.insert_at(params[:position].to_i)
head :ok
end
private
# Use callbacks to share common setup or constraints between actions.
def set_task
@task = Task.find(params[:id])
end
def set_project
@project = Project.find(params[:project_id])
end
# Only allow a list of trusted parameters through.
def task_params
params.require(:task).permit(:title, :position, :description, :project_id, :user_id, :status, :completed)
end
end
Task.rb
class Task < ApplicationRecord
belongs_to :project
belongs_to :user
acts_as_list
after_update_commit {broadcast_replace_to "tasks", locals: {task: self}}
end
Any help is appreciated!