4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Rails7でdestroyが効かない

Posted at

メモ

サンプルなどで記事を削除する際に使用する機能がつかえなかったので。

  • Rails5の書き方
<%= link_to '削除', board ,method: :delete, class: "btn btn-outline-dark" %>
  • Raisl7の書き方
<%= link_to '削除', board ,method: :post, 
  data: {
    turbo_method: :delete,
    turbo_confirm: "Are you sure?"
}, class: "btn btn-outline-dark" %>

旧バージョンではJQueryを使ってたけど、新バージョンでは使うJSライブラリが変わったとかなんとか。

書き方を変えても削除できず、showのページがでてしまう。
メソッドがGETで送られてしまうとかなんとか。

参考ページの一番下に合った方法で試すと削除できた!

application.html.erbに下記を追記

<%= javascript_include_tag "turbo", type: "module" %>

たぶんこのgemなどは事前にインストールが必要っぽい。

# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
gem "importmap-rails"

# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem "turbo-rails"

# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem "stimulus-rails"

参考ページ

Guide v7.0.3.1 “link_to “Destroy” “turbo_method: :delete” doesnt works / Ubuntu 22.04

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?