I’m following the Rails official Getting started guide and ran into this error when trying to delete:
AbstractController::ActionNotFound (The action 'destroy' could not be found for TasksController)
All other actions (index, show, new, create, edit, update) work correctly.
I can see that the request is POST and has a ?_method=delete
url param. Is this normal? Shouldn’t it be a DELETE request?
My model is named task
, the controller is named tasks_controller
.
Inside routes.rb I have resources :tasks
.
In the erb I have this link with turbo delete:
<%= link_to "Delete", task_path(@task), data: { turbo_method: :delete, turbo_confirm: "Are you sure?" } %>
All ‘task’ routes:
root_path GET /
tasks#index
/mnt/e/Work/later/config/routes.rb:3
tasks_path GET /tasks(.:format)
tasks#index
/mnt/e/Work/later/config/routes.rb:5
POST /tasks(.:format)
tasks#create
/mnt/e/Work/later/config/routes.rb:5
new_task_path GET /tasks/new(.:format)
tasks#new
/mnt/e/Work/later/config/routes.rb:5
edit_task_path GET /tasks/:id/edit(.:format)
tasks#edit
/mnt/e/Work/later/config/routes.rb:5
task_path GET /tasks/:id(.:format)
tasks#show
/mnt/e/Work/later/config/routes.rb:5
PATCH /tasks/:id(.:format)
tasks#update
/mnt/e/Work/later/config/routes.rb:5
PUT /tasks/:id(.:format)
tasks#update
/mnt/e/Work/later/config/routes.rb:5
DELETE /tasks/:id(.:format)
tasks#destroy
/mnt/e/Work/later/config/routes.rb:5
Full controller code:
class TasksController < ApplicationController
def index
@tasks = Task.all
end
def show
@task = Task.find(params[:id])
end
def new
@task = Task.new
end
def create
@task = Task.new(task_params)
if @task.save
redirect_to @task, notice:"Task was successfully created"
else
render :new, status: :unprocessable_entity
end
def edit
@task = Task.find(params[:id])
end
def update
@task = Task.find(params[:id])
if @task.update(task_params)
redirect_to @task, notice:"Task was successfully updated"
else
render :edit, status: :unprocessable_entity
end
end
def destroy
@task = Task.find(params[:id])
@task.destroy
redirect_to root_path, status: :see_other
end
end
private
def task_params
params.require(:task).permit(:title, :body)
end
end