実現したいこと
削除ボタンを押したら削除したい。
発生している問題・分からないこと
削除ボタンを押したら削除したいのですが、RoutingErrorになってしまいます。
エラーメッセージ
error
1 ActionController::RoutingError (No route matches [GET] "/tasks/1"):
該当のソースコード
tasks_controller.rb
1class TasksController < ApplicationController 2 def index 3 @tasks = Task.all 4 end 5 6 def new 7 8 end 9 10 def create 11 Task.create(title:params[:title]) 12 redirect_to "/tasks" 13 end 14 15 def destroy 16 @task = Task.find(params[:id]) 17 puts "Destroying task with title: #{@task.title}" 18 @task.destroy 19 redirect_to "/tasks" 20 end 21 22end 23
routes.rb
1Rails.application.routes.draw do 2 get "tasks" => "tasks#index" 3 get "tasks/new" => "tasks#new" 4 post "tasks/create" => "tasks#create" 5 delete "tasks/:id" => "tasks#destroy" 6end 7
new.html.erb
1<h2>Create New Task</h2> 2<%= form_with url:"/tasks/create", local:true do |f| %> 3 <p><%= f.text_field :title %></p> 4 <p><%= f.submit "Create" %></p> 5<% end %>
index.html.erb
1<h2>To Do App</h2> 2<%= link_to "Create New Task", "/tasks/new" ,class:"btn btn-primary"%> 3<h2>【Task List】</h2> 4<% @tasks.each do |task| %> 5 <p>・<%= task.title %><%= link_to "/tasks/" + task.id.to_s,method: :delete do %> 6 <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash" viewBox="0 0 16 16"> 7 <path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5m2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5m3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0z"/> 8 <path d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4zM2.5 3h11V2h-11z"/> 9</svg> 10<% end %> 11 </p> 12<% end %>
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
ChatGPTに聞きましたが、その対応をしても治りませんでした。
補足
特になし

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。