LoginSignup
2
0

More than 1 year has passed since last update.

Rails7のdeviseで、link_toでログアウトできない

Posted at

現象

Rails7でdeviseによる認証を追加した際、ログアウトのリンク作成する場合、

= link_to 'ログアウト', destroy_user_session_path, method: :delete

ではうまく動きませんでした。

エラー

No route matches [GET] "/users/sign_out"
スクリーンショット 2023-02-12 17.47.23.png

とのことなので、DELETEのつもりがGETになってしまっているようですね。

解決方法

button_toに変更すれば良いという記事もありましたが、link_toのままでやりたい場合は、
次のように指定してあげるとうまく動くようです。

= link_to 'ログアウト', destroy_user_session_path, data: { turbo_method: :delete }

ログアウトのリンクだけでなく、データの削除リンクも同じパターンで出来そうですね✨

2
0
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
2
0