実現したいこと
エラー「No route matches [GET] 」を解決して投稿を削除できるようにしたい
前提
開発中のrailsアプリに搭載する投稿記事の削除機能を作っています。
記事を削除後に記事の一覧ページ(growth_diaries/index)に遷移するようにしたいです。
削除するコードを書き、削除用のリンクを設置して押したところ下記のようなエラーメッセージが出て削除できない状態です。
関連するMVCをチェックしましたがどこにもおかしいところが見当たらないため、自分ではわからない状態です。ご教授いただけますと幸いです。
発生している問題・エラーメッセージ
下記のようにルートがないという内容のエラーが出ます。
No route matches [GET] "/growth_diaries/7/destroy"
該当のソースコード
config/routes.rb
ruby
1Rails.application.routes.draw do 2 get 'jobs/index' => "jobs#index" 3 get 'jobs/:id' => "jobs#show" 4 get 'farmers/index' 5 get 'users/index' 6 get 'users/new' 7 get 'home/top' 8 get 'growth_diaries/new' => "growth_diaries#new" 9 get 'growth_diaries/index' => "growth_diaries#index" 10 get 'growth_diaries/:id' => "growth_diaries#show" 11 get 'growth_diaries/:id/edit' => "growth_diaries#edit" 12 post 'growth_diaries/:id/update' => "growth_diaries#update" 13 post 'growth_diaries/:id/destroy' => "growth_diaries#destroy" 14 post 'growth_diaries/create' => "growth_diaries#create" 15end
/app/controllers/growth_diaries_controller.rb
ruby
1class GrowthDiariesController < ApplicationController 2 3 def new 4 end 5 6 def create 7 @growth_diary = GrowthDiary.new(title: params[:title], body: params[:body]) 8 @growth_diary.save 9 redirect_to("/growth_diaries/index") 10 end 11 12 def show 13 @growth_diary = GrowthDiary.find_by(id: params[:id]) 14 end 15 16 def index 17 @growth_diaries = GrowthDiary.all.order(created_at: :desc) 18 end 19 20 def edit 21 @growth_diary = GrowthDiary.find_by(id: params[:id]) 22 end 23 24 def update 25 @growth_diary = GrowthDiary.find_by(id: params[:id]) 26 @growth_diary.title = params[:title] 27 @growth_diary.body = params[:body] 28 @growth_diary.save 29 redirect_to("/growth_diaries/index") 30 end 31 32 def destroy 33 @growth_diary = GrowthDiary.find_by(id: params[:id]) 34 @growth_diary.destroy 35 redirect_to("/growth_diaries/index") 36 end 37end
app/views/growth_diaries/show.html.erb
ruby
1<h1><%= @growth_diary.title %></h1> 2<h2><%= @growth_diary.body %></h2> 3<p><%= link_to("編集", "/growth_diaries/#{@growth_diary.id}/edit") %></p> 4<%= link_to("削除", "/growth_diaries/#{@growth_diary.id}/destroy", {method: "post"}) %>
試したこと
参考にしているProgateとも見比べましたが、おかしなところが見当たらない状態です。
- スペルの確認
- ルーティングの確認(「スラッシュ」と「#」の打ち間違いがないかなど)
補足情報(FW/ツールのバージョンなど)
Rails 7.0.4
