railsのmethod: :deleteを指定しているのに詳細ページに飛んでしまう

実現したいこと

掲示板一覧画面にある削除ボタンを押下すると指定した投稿が削除されるようにしたい。

前提

dockerを使用したrailsの環境で掲示板の作成をしています。
削除機能を実装中に削除ボタンを押下すると詳細ページに飛んでしまって削除ができないという現象が発生しました。

発生している問題・エラーメッセージ

ログを確認するとgetが呼ばれています。

Started GET "/boards" for 192.168.65.1 at 2023-11-28 06:48:23 +0000 Cannot render console from 192.168.65.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1 Processing by BoardsController#index as HTML Rendering layout layouts/application.html.erb Rendering boards/index.html.erb within layouts/application Board Load (0.6ms) SELECT `boards`.* FROM `boards` ↳ app/views/boards/index.html.erb:20 Rendered boards/index.html.erb within layouts/application (Duration: 5.3ms | Allocations: 1929) Rendered layout layouts/application.html.erb (Duration: 33.7ms | Allocations: 15141) Completed 200 OK in 37ms (Views: 34.1ms | ActiveRecord: 0.6ms | Allocations: 15409)

該当のソースコード

routes.rb

1Rails.application.routes.draw do 2 # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html 3 4 # Defines the root path route ("/") 5 resources :boards 6end 7

boards_controller.rb

1class BoardsController < ApplicationController 2 def index 3 @boards = Board.all 4 end 5 6 def new 7 @board = Board.new 8 end 9 10 def create 11 board = Board.create(board_params) 12 redirect_to board 13 end 14 15 def show 16 @board = Board.find(params[:id]) 17 end 18 19 def edit 20 @board = Board.find(params[:id]) 21 end 22 23 def update 24 board = Board.find(params[:id]) 25 board.update(board_params) 26 27 redirect_to board 28 end 29 30 def destroy 31 board = Board.find(params[:id]) 32 board.delete 33 34 redirect_to boards_path 35 end 36 37 private 38 39 def board_params 40 params.require(:board).permit(:author_name, :title, :body) 41 end 42 43end 44

index.html.erb

1<div class="d-flex align-items-center"> 2 <h1>掲示板一覧</h1> 3 <div class="ms-auto boards__linkBox"> 4 <%= link_to '新規作成', new_board_path, class: 'btn btn-outline-dark' %> 5 </div> 6</div> 7<table class="table table-hover boards__table"> 8 <thead class="table-dark"> 9 <tr> 10 <th>ID</th> 11 <th>タイトル</th> 12 <th>作成者</th> 13 <th>作成日時</th> 14 <th>更新日時</th> 15 <th></th> 16 <th></th> 17 </tr> 18 </thead> 19 <tbody> 20 <% @boards.each do |board| %> 21 <tr> 22 <td><%= board.id %></td> 23 <td><%= board.title %></td> 24 <td><%= board.author_name %></td> 25 <td><%= board.created_at.to_formatted_s(format = :datetime_jp) %></td> 26 <td><%= board.updated_at.to_formatted_s(format = :datetime_jp) %></td> 27 <td><%= link_to '詳細', board, class: 'btn btn-outline-dark' %></td> 28 <td><%= link_to '削除', board, class: 'btn btn-outline-dark', method: :delete %></td> 29 </tr> 30 <% end %> 31 </tbody> 32</table>

試したこと

application.jsに//= require rails-ujsを追記するという記事を見つけたので試してみましたが、変わりませんでした。

ご教授よろしくお願いします。

コメントを投稿

0 コメント