LoginSignup
7
4

More than 3 years have passed since last update.

[Rails] 削除ボタンを押した時に「本当に削除しますか?」のアラートを表示したい

Posted at

解決したいこと

投稿などの削除をボタンを押した時に、いきなり削除されるのではなく、
確認のダイアログを表示してから削除を実行したい。

解決方法

link_to メソッドにdata: {confirm:""} を追加する。

html.erb
<%= link_to post_path(@post.id), method: :delete, data: {confirm: "削除しますか?"} %>

上のようにlink_to にdata: {confirm: "表示したい文章"} を追加すると、
alert.png
削除ボタンを押した時に、こんな風に確認ダイアログが出てきてくれます。
あっという間にできてしまって感動です。

参考資料
https://railsdoc.com/page/link_to

7
4
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
7
4