Rails 7 - Rails は DELETE アクションを GET として考慮します
概要
Operations モデルがあり、delete メソッドを使用してレコードを削除しようとしています。しかし、ルーティングエラーが発生します。 Rails が削除アクションを Get として考慮している理由が理解できません。以下はエラーメッセージです。
エラー: [GET] “/home/delete_operation/1” に一致するルートはありません
以下は私が設定したロジックです。
ビュー:
<%= link_to 'Delete operation', home_delete_operation_path(id: operation.id), method: :delete, data: { confirm: 'Are you sure?', turbo: false }, class: "btn btn-dark btn-sm rounded-0" %>
コントローラ:
def delete_operation
@operation = Operation.find(params[:id])
@operation.destroy
end
ルート:
delete 'home/delete_operation/:id', to: 'home#delete_operation', as: :home_delete_operation
Rails はルーティングに JS を使用していると読みました。したがって、Application.jsにコードを添付します
アプリケーション.js
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import "@hotwired/turbo-rails"
import "controllers"
//= require jquery3
//= require popper
//= require bootstrap-sprockets
//= require jquery_ujs
//= require chartkick
//= require Chart.bundle
import "chartkick"
import "Chart.bundle"
解決策
既存のデータ: {confirm: ‘Are you some?’,turbo: false } を次のデータに置き換えました: {turbo_method: :delete,turbo_confirm: ‘Are you some?’ link_to タグに } を追加すると機能しました。